I am writing an application in FireMonkey for Android and iOS. I would like to open a file from a URL in the default app on my phone (it could be a PDF, DOC, JPG, etc).
On Android, I do it like this:
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setData(StrToJURI(URL));
SharedActivity.StartActivity(Intent);
How can I do something like this on iOS?
I used this code in my last project. Work fine on any platform.
unit u_urlOpenUnit;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
{$IF Defined(IOS)}
macapi.helpers, iOSapi.Foundation, FMX.helpers.iOS;
{$ELSEIF Defined(ANDROID)}
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net,
Androidapi.JNI.App,
Androidapi.helpers;
{$ELSEIF Defined(MACOS)}
Posix.Stdlib;
{$ELSEIF Defined(MSWINDOWS)}
Winapi.ShellAPI, Winapi.Windows;
{$ENDIF}
type
tUrlOpen = class
class procedure Open(const URL: string; const DisplayError: Boolean = False);
end;
implementation
class procedure tUrlOpen.Open(const URL: string; const DisplayError: Boolean = False);
{$IF Defined(ANDROID)}
var
Intent: JIntent;
{$ENDIF}
begin
{$IF Defined(ANDROID)}
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(URL)));
try
TAndroidHelper.Activity.startActivity(Intent);
except
on e: Exception do
begin
// if DisplayError then ShowMessage('Error: ' + e.Message);
// exit(false);
end;
end;
{$ELSEIF Defined(MSWINDOWS)}
ShellExecute(0, 'OPEN', PWideChar(URL), nil, nil, SW_SHOWNORMAL);
{$ELSEIF Defined(IOS)}
if SharedApplication.canOpenURL(StrToNSUrl(URL)) then
SharedApplication.OpenURL(StrToNSUrl(URL));
{$ELSEIF Defined(MACOS)}
_system(PAnsiChar('open ' + AnsiString(URL)));
{$ENDIF}
end;
end.