【问题标题】:How to open file in default app on iOS using FireMonkey如何使用 FireMonkey 在 iOS 上的默认应用程序中打开文件
【发布时间】:2020-12-02 15:32:02
【问题描述】:

我正在用 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 上做这样的事情?

【问题讨论】:

  • StrToJURI(URL) URL 是什么样的?这不适用于 Android N/7+。
  • 我不明白你需要为 iO 编写不同的代码,因为我知道你会同时为 Windows、Android 和 iO 编写代码。
  • @blackapps 虽然 Firemonkey 是一个跨平台的框架,但它不能以抽象的方式做所有事情。在默认应用程序中打开文件就是其中之一。为此必须使用特定于平台的 API。这意味着在 Windows 上使用 ShellExecute(),在 Android 上使用 Intent,在 iOS 上使用 SharedApplication.OpenURL(),等等。

标签: android ios delphi firemonkey


【解决方案1】:

我在上一个项目中使用了此代码。在任何平台上都能正常工作。

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.

【讨论】:

  • 谢谢!正是我需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
相关资源
最近更新 更多