uses
{$IFDEF ANDROID}
Androidapi.Helpers,
FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net, Androidapi.JNI.JavaTypes;
{$ENDIF}
{$IFDEF IOS}
Macapi.Helpers, iOSapi.Foundation, FMX.Helpers.iOS,
{$ENDIF}
IdURI, SysUtils, Classes, FMX.Dialogs;
// URLEncode is performed on the URL
// so you need to format it protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
implementation
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(URL))));
try
SharedActivity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end;
{$ELSE}
{$IFDEF IOS}
var
NSU: NSUrl;
begin
// iOS doesn't like spaces, so URL encode is important.
NSU := StrToNSUrl(TIdURI.URLEncode(URL));
if SharedApplication.canOpenURL(NSU) then
exit(SharedApplication.openUrl(NSU))
else
begin
if DisplayError then
ShowMessage('Error: Opening "' + URL + '" not supported.');
exit(false);
end;
end;
{$ELSE}
begin
raise Exception.Create('Not supported!');
end;
{$ENDIF IOS}
{$ENDIF ANDROID}
// URLEncode is performed on the URL
// so you need to format it protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
implementation
uses
{$IFDEF ANDROID}
IdURI, SysUtils, Classes, FMX.Dialogs,
FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net,Androidapi.Helpers,Androidapi.JNI.JavaTypes;
{$ENDIF}
{$IFDEF IOS}
IdURI, SysUtils, Classes, FMX.Dialogs,
Macapi.Helpers, iOSapi.Foundation, FMX.Helpers.iOS;
{$ENDIF}
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(URL))));
try
SharedActivity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end;
{$ENDIF}
{$IFDEF IOS}
var
NSU: NSUrl;
begin
// iOS doesn't like spaces, so URL encode is important.
NSU := StrToNSUrl(TIdURI.URLEncode(URL));
if SharedApplication.canOpenURL(NSU) then
exit(SharedApplication.openUrl(NSU))
else
begin
if DisplayError then
ShowMessage('Error: Opening "' + URL + '" not supported.');
exit(false);
end;
end;
{$ENDIF}
{$IF not Declared(_PU)}
const
// On Mac OSX, cdecl names have a preceeding underscore
// if x86 native backend.
{$IF Defined(UNDERSCOREIMPORTNAME)}
_PU = '_';
{$ELSE}
_PU = '';
{$ENDIF}
{$EXTERNALSYM _PU}
{$ENDIF}
function GetEncodeURL(const URL: string; Https:Boolean = True): string;
begin
Result := URL;
if FileExists(Result) then
begin
{$IFDEF MSWINDOWS}
{$ELSE}
if Result.Substring(1, 1) <> '/' then
begin
Result := '/' + Result;
end;
Result := 'file://' + Result;
{$ENDIF}
end
else
begin
if Https then
begin
Result := 'https://' + URL;
end
else
begin
Result := 'https://' + URL;
end;
Result := TIdURI.URLEncode(Result);
end;
end;
function OpenFileOnExtApp(const FileName: string; Https:Boolean = True): Boolean;
{$IFDEF ANDROID}
var
Intent: JIntent;
FileExtension: string;
mime: JMimeTypeMap;
MIMEType: JString;
FileToOpen: string;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Result := False;
FileExtension := AnsiLowerCase(ExtractFileExt(FileName));
mime := TJMimeTypeMap.JavaClass.getSingleton();
MIMEType := nil;
if mime <> nil then
begin
MIMEType := mime.getMimeTypeFromExtension(StringToJString(FileExtension));
end;
if MIMEType <> nil then
begin
// 调用相应程序打开当前程序
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
FileToOpen := GetEncodeURL(FileName, Https);
Intent.setDataAndType(StrToJURI(FileToOpen), MIMEType);
try
SharedActivity.startActivity(Intent);
Result := True;
except
end;
end;
end;
{$ELSE}
{$IFDEF IOS}
var
NSU: NSUrl;
AURL: string;
begin
Result := False;
AURL := GetEncodeURL(FileName, Https);
// iOS doesn't like spaces, so URL encode is important.
NSU := StrToNSUrl(AURL);
if TOSVersion.Check(9) or SharedApplication.canOpenURL(NSU) then
try
Result := SharedApplication.openUrl(NSU);
except
end;
end;
{$ELSE}
{$IFDEF MSWINDOWS}
var
AURL: string;
begin
Result := False;
AURL := GetEncodeURL(FileName, Https);
try
ShellExecute(GetActiveWindow, 'open', PChar(AURL), '', '', SW_MAXIMIZE);
Result := True;
except
end;
end;
{$ELSE}
var
M:TMarshaller;
AURL: string;
begin
Result := False;
//AURL := 'open -a Safari ' + GetEncodeURL(AURL);
AURL := 'open ' + GetEncodeURL(FileName, Https);
try
_system(M.AsAnsi(AURL, CP_UTF8).ToPointer);
Result := True;
except
end;
// raise Exception.Create('Not supported!');
end;
{$ENDIF MSWINDOWS}
{$ENDIF IOS}
{$ENDIF ANDROID}[/mw_shl_code]