android - 如何使用 FireMonkey 在 iOS 上的默认应用程序中打开文件

标签 android ios delphi firemonkey

我正在 FireMonkey 中为 Android 和 iOS 编写一个应用程序。我想从手机上默认应用程序中的 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.

关于android - 如何使用 FireMonkey 在 iOS 上的默认应用程序中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65111171/

相关文章:

delphi - Indy 的 TidHTTPServer 与 Delphi 的 TDSHTTPService 的推荐或比较

c# - 哪个是 delphi "class of "(类的类型)的 C# 声明?

android - 我对此Android Studio Kotlin程序有疑问:

ios - UIBarButtonItem 是用淡色着色的。我可以按原样显示吗?

objective-c - 重复使用时如何完全清除单元格?

ios - 是否可以为第一个字符手动创建 UITextRange?

android - 如何在 Delphi 中禁用显示建议

android - BroadcastReceiver不接收来自外部链接的Intent

Android 随着时间的推移减慢 TranslateAnimation

java - 返回值 android 中 Intent 的最佳实践