Delphi XE5如何透過URL呼叫Android或iOS上的其他程式?

之前曾經分享過,手機版的Line可以透過 line://msg/<CONTENT TYPE>/<CONTENT KEY> 這樣的語法呼叫Line 的程式分享。

當然Delphi XE5也可以透過程式呼叫程式才對,在 的「Sending a URL to Another App on Android and iOS with Delphi XE5」文章有教您如何呼叫

  • http, tel, sms, fb, mailto, twitter, geo…..
  • 作者也分類成三種:
  • 1. iOS跟Android都可以使用的URLs
  • http://superlevin.ifengyuan.tw/
  • tel://0921789779
  • sms://hello_world
  • http://twitter.com/superlevin (這個語法會直接打開Android上的Twitter Client程式)
  • mailto://superlevin@gmail.com
  • twitter://user?screen_name=superlevin
  • fb://profile/705666540
    (可以透過 http://graph.facebook.com/(帳號)superlevin取得UID)

iOS特殊語法

  • http://maps.apple.com?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066 (需要URL encode)

Android特殊語法

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

作者在文中有提到使用TidURL.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}
  FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Net, Androidapi.JNI.JavaTypes;
{$ELSE}
{$IFDEF IOS}
  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.

我要留言

留言

One thought on “Delphi XE5如何透過URL呼叫Android或iOS上的其他程式?

  1. 感謝版主提供詳細的說明,
    經本人實際於 XE6 測試 OpenViewUrl.pas 修改編譯後,Android 及 iOS 可用部份如下
    OpenURL(‘http://Google.com’); //Android 可用,iOS 可用
    OpenURL(‘mailto://xxxx2002@gmail.com’); //Android 可用,iOS 不可用
    OpenURL(‘tel://09263505xx’); //Android 可用,iOS 不可用
    OpenURL(‘sms://hello_world’); //Android 可用,iOS 不可用
    OpenURL(‘fb://profile/705666540’); //Android 不可用,iOS 不可用
    OpenURL(‘line://xxxxx’); //Android 不可用,iOS 不可用
    OpenURL(‘geo://0,0?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066’); //Android 可用,iOS 不可用
    OpenURL(‘geo://46.191200, -122.194400’); //Android 可用,iOS 不可用
    OpenURL(‘content://contacts/people/’); //Android 可用,iOS 不可用,帶出聯絡人 APP
    OpenURL(‘content://contacts/people/1’); //Android 可用,iOSe 不可用,帶出聯絡人 APP 第一筆資料
    OpenURL(‘http://maps.apple.com?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066’);
    //Android 可用,iOS 可用
    修改後之 OpenViewUrl.pas

    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 and Macapi.Helpers
    FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
    Androidapi.JNI.Net, Androidapi.JNI.JavaTypes, Androidapi.Helpers;
    {$ELSE}
    {$IFDEF IOS}
    iOSapi.Foundation, FMX.Helpers.iOS, Macapi.Helpers;
    {$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.

發表迴響

你的電子郵件位址並不會被公開。