Categories
Android Conferences iOS Mobile

Sending a URL to Another App on Android and iOS with Delphi XE5

Here is the source code for my Open and View URL library from my CodeRage 8 session “Beyond the App”. Here is a download of the example app. I’ll see about posting it to a SVN repository too so it can grow and evolve. Thanks to Al Mannarino for his code that started this!

Note the code is using TidURL.URLEncode on all URLs. I found it is only required on the maps for iOS (iOS doesn’t like spaces) but may be causing trouble with the geo:// on Android.

Some example protocols

  • http, tel, sms, fb, mailto, twitter, geo, etc.

Example URLs

  • – Common to both iOS & Android –
  • http://www.embarcadero.com/
  • tel://(415)834-3131
  • sms://1234
  • http://twitter.com/coderage (This opens with the Twitter client on Android)
  • mailto://jim.mckeeth@embarcadero.com
  • twitter://user?screen_name=coderage
  • fb://profile/34960937498 (get the UID from http://graph.facebook.com/embarcaderotech or for whatever page you are looking for)
  • – iOS Specific –
  • http://maps.apple.com?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066 (this needs the URL encode – Apple has some additional APIs that are recommended.)
  • – Android Only –
  • content://contacts/people/
  • content://contacts/people/1
  • geo://0,0?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066
  • geo://46.191200, -122.194400 (I think this one doesn’t like the URLEncode)
unit OpenViewUrl;

interface

// URLEncode is performed on the URL
// so you need to format it   protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;

implementation

uses
  IdURI, SysUtils, Classes, FMX.Dialogs,
{$IFDEF ANDROID}
  Androidapi.Helpers,
  FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Net, Androidapi.JNI.JavaTypes;
{$ELSE}
{$IFDEF IOS}
  Macapi.Helpers, iOSapi.Foundation, FMX.Helpers.iOS;
{$ENDIF IOS}
{$ENDIF ANDROID}

function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
var
  Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
    TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(URL))));
  try
    SharedActivity.startActivity(Intent);
    exit(true);
  except
    on e: Exception do
    begin
      if DisplayError then ShowMessage('Error: ' + e.Message);
      exit(false);
    end;
  end;
end;
{$ELSE}
{$IFDEF IOS}
var
  NSU: NSUrl;
begin
  // iOS doesn't like spaces, so URL encode is important.
  NSU := StrToNSUrl(TIdURI.URLEncode(URL));
  if SharedApplication.canOpenURL(NSU) then
    exit(SharedApplication.openUrl(NSU))
  else
  begin
    if DisplayError then
      ShowMessage('Error: Opening "' + URL + '" not supported.');
    exit(false);
  end;
end;
{$ELSE}
begin
  raise Exception.Create('Not supported!');
end;
{$ENDIF IOS}
{$ENDIF ANDROID}

end.

25 replies on “Sending a URL to Another App on Android and iOS with Delphi XE5”

Hi!
Thank you so much for sharing your code, very useful!
Maybe you can help me?
In an Android system, I need to open a web page, login, navigate in order to find something and after that, send the actual url at my app.
I can’t use a TwebBrowser because seem impossible enable the keyboard for the edit field (damn login!!)

How can we play a song URL in delphi XE5 with help of Media Player component or Web Browser component, but without redirecting on any default browser,
Is there any path to localize the URL

Hi!
thanks for sharing, it works great with website etc but when using the mailto, somewhere it remove teverything before and uincuding the @ of the mailto email address, had anyone else had this happen?

I use the function like this
if not OpenURL(‘mailto://info@2agree.nl?subject=TestEmail&body=testbody’, true) then

hi, on XE7 i have
[DCC Error] OpenViewUrl.pas(31): E2003 Undeclared identifier: ‘StringToJString’
[DCC Error] OpenViewUrl.pas(33): E2003 Undeclared identifier: ‘SharedActivity’

Thanks for help

Can i open my app when user click a link in twitter? thats mean, example : i post a twitt and has a link.i want user open my app when click the this link of course if my app installed. otherwise go to store.

To fix it for mail on Android you only should put “mailto:” in front of the email address and change code to:

Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(URL)));

Hello I can’t send a message with whatsapp with IOS, can you tell me the instruction to call back?

If I use the code below I get the following error – “Host field is empty”

Var
sPath : String;
begin
sPath := TPath.Combine(TPath.GetPublicPath, ‘test1.pdf’);
if FileExists(sPath) = true then
begin
OpenURL(‘file://’+sPath);
end else
Showmessage(‘file does not exist! ‘ + sPath);

Thanks

Comments are closed.