如何使用 FireMonkey 在 iOS 上的默认应用程序中打开文件
How to open file in default app on iOS using FireMonkey
我正在 FireMonkey 中为 Android 和 iOS 编写一个应用程序。我想在 phone 的默认应用程序中打开来自 URL 的文件(它可以是 PDF、DOC、JPG 等)。
在 Android,我是这样做的:
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setData(StrToJURI(URL));
SharedActivity.StartActivity(Intent);
我怎样才能在 iOS 上做这样的事情?
我在上一个项目中使用了这段代码。在任何平台上都能正常工作。
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.
我正在 FireMonkey 中为 Android 和 iOS 编写一个应用程序。我想在 phone 的默认应用程序中打开来自 URL 的文件(它可以是 PDF、DOC、JPG 等)。
在 Android,我是这样做的:
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setData(StrToJURI(URL));
SharedActivity.StartActivity(Intent);
我怎样才能在 iOS 上做这样的事情?
我在上一个项目中使用了这段代码。在任何平台上都能正常工作。
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.