How to open an URL with the default browser with FireMonkey cross-platform applications?

此生再无相见时 提交于 2019-12-03 07:34:26

问题


Usually, I use: ShellExecute(0, 'OPEN', PChar(edtURL.Text), '', '', SW_SHOWNORMAL);

How can I have the same behaviour (opening a link in the default browser), on all platforms (Windows and OSX)?


回答1:


In the FireMonkey discussion forum I found this code for a question about NSWorkspace.URLForApplicationToOpenURL:

uses
  Posix.Stdlib;
....
  _system(PAnsiChar('open ' + ACommand));

(not tested by me)


Update: Posix is not available on Windows so it is not possible to write a solution which uses the same OS calls on all platforms. I suggest to move such code in a central 'XPlatform' unit which has some IFDEF POSIX etc.




回答2:


Regarding the answer of mjn, I have written the following unit. I have successfully tested it on Windows but I don't have an OSX to test it on this platform. If someone can confirm it works, I'd appreciate.

unit fOpen;

interface

uses
{$IFDEF MSWINDOWS}
  Winapi.ShellAPI, Winapi.Windows;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
  Posix.Stdlib;
{$ENDIF POSIX}

type
  TMisc = class
    class procedure Open(sCommand: string);
  end;

implementation

class procedure TMisc.Open(sCommand: string);
begin
{$IFDEF MSWINDOWS}
  ShellExecute(0, 'OPEN', PChar(sCommand), '', '', SW_SHOWNORMAL);
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
  _system(PAnsiChar('open ' + AnsiString(sCommand)));
{$ENDIF POSIX}
end;

end.

and I call it like this:

TMisc.Open('https://stackoverflow.com/questions/7443264/how-to-open-an-url-with-the-default-browser-with-firemonkey-cross-platform-applic');



回答3:


For all platforms (Windows, macOs, iOS and Android) you can use the unit I wrote for my blog

unit u_urlOpen;

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(URL: string);
  end;

implementation

class procedure tUrlOpen.Open(URL: string);
{$IF Defined(ANDROID)}
var
  Intent: JIntent;
{$ENDIF}
begin
{$IF Defined(ANDROID)}
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  Intent.setData(StrToJURI(URL));
  tandroidhelper.Activity.startActivity(Intent);
  // SharedActivity.startActivity(Intent);
{$ELSEIF Defined(MSWINDOWS)}
  ShellExecute(0, 'OPEN', PWideChar(URL), nil, nil, SW_SHOWNORMAL);
{$ELSEIF Defined(IOS)}
  SharedApplication.OpenURL(StrToNSUrl(URL));
{$ELSEIF Defined(MACOS)}
  _system(PAnsiChar('open ' + AnsiString(URL)));
{$ENDIF}
end;

end.



回答4:


XE2 C++ code that is tested succesfully (Windows 7 64 and OSX Lion), minor improvements. Thank's Whiler, the pain is over :)

#include <fmx.h>
// ---------------------------------------------------------------------------
#ifdef _WIN32
#include <shellapi.h>
#endif// Windows
#ifdef TARGET_OS_MAC
#include <Posix.Stdlib.hpp>
#endif // Mac

void OpenCommand(String _sCommand) {
    #ifdef _Windows
    String open = "open";
    ShellExecute(0, open.w_str(), _sCommand.c_str(), NULL, NULL, SW_SHOWNORMAL);
    #endif // Windows

    #ifdef TARGET_OS_MAC
    system(AnsiString("open " + AnsiString(_sCommand)).c_str());
    #endif // Mac
}



回答5:


As @NicoBlu mentioned, the accepted solution seems to truncate the URL after the first occurence of '&'. Here is what works for me without truncation:

uses Macapi.AppKit, Macapi.Foundation, Macapi.Helpers;

// ...

procedure OpenLinkInDefaultBrowser(const Link: string);
  var URL : NSURL;
      Workspace : NSWorkspace;
begin
  URL := TNSURL.Wrap(TNSURL.OCClass.URLWithString(StrToNSStr(Link)));
  Workspace := TNSWorkspace.Wrap(TNSWorkspace.OCClass.sharedWorkspace);
  Workspace.openURL(URL);
end;



回答6:


And now a C++ version (OSx code untested, also not sure about the _POSIX #def):

#ifdef _Windows
#include <Winapi.Windows.hpp>
#endif // _Windows
#ifdef _POSIX
#include <Posix.Stdlib.h>
#endif // _POSIX

void OpenCommand(String _sCommand)
{
    #ifdef _Windows
    ShellExecute(0, _T("open"), _sCommand.c_str(), _T(""), _T(""), SW_SHOWNORMAL);
    #endif // _Windows
    #ifdef _POSIX
    _system(AnsiString("open " + AnsiString(_sCommand)).c_str());
    #endif // _POSIX
}



回答7:


_system(PAnsiChar('open ' + AnsiString(sCommand)));

not works if URL string (sCommand) includes ampersand char (&), necessary to specify many arguments in querystring.

URL sended to def. browser in OSX (Safari) is truncated at the first occurence of &.




回答8:


LEncodedString : String;

begin
    LEncodedString := TIdURI.URLEncode('http://www.malcolmgroves.com');
    sharedApplication.openURL(StringToNSURL(LEncodedString));
end;


来源:https://stackoverflow.com/questions/7443264/how-to-open-an-url-with-the-default-browser-with-firemonkey-cross-platform-appli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!