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

37,310

Solution 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.

Solution 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');

Solution 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.

Solution 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
}

Solution 5

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
}
Share:
37,310
Whiler
Author by

Whiler

My freewares My French blog

Updated on August 23, 2020

Comments

  • Whiler
    Whiler over 3 years

    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)?