Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

C++BuilderCode SnippetDelphiFiremonkeyIOS

Learn How To Open A Delphi FireMonkey App With A Custom URL Scheme On IOS

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSIt is surprisingly easy to launch your FireMonkey app on IOS from another app and pass it some basic settings. I have tested this solution on a recent IPad and IOS 11.4. This functionality is provided by Apple and you can easily modify your Delphi FireMonkey app to register it’s own URL Scheme and handle the incoming settings. The first thing you need to do is edit your info.plist.TemplateiOS.xml file in your project directory. Under the <%ExtraInfoPListKeys%> line you should add the following:

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>myappurlschemename</string>
</array>
</dict>

Be sure to replace the myappurlschemename with the actual name of your app or at least the name of the scheme you want to register and use to launch your app. It gets used to launch the app in a URL like this: myappurlschemename://mypage?myname=myvalue You can paste myappurlschemename://mypage?myname=myvalue into Safari to test launching your app. There is also a good tutorial here for XCode so you can see how it works from the XCode side.

Next up you need to add some code to your app to handle the TApplicationEvent.OpenURL event. You can do this with the following code. You will want to add FMX.Platform to your uses section and you will want to start listening to the FMXApplicationEventService in your TMainForm.OnCreate event:

uses
FMX.Platform,
{$IFDEF IOS}
FMX.Platform.iOS
{$ENDIF}

procedure TMainForm.FormCreate(Sender: TObject);
var
  FMXApplicationEventService: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService
    (IFMXApplicationEventService, IInterface(FMXApplicationEventService)) then
    FMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent);
end;

function TMainForm.HandleAppEvent(AAppEvent: TApplicationEvent;
  AContext: TObject): Boolean;
begin
  case AAppEvent of
     TApplicationEvent.OpenURL:
     begin
  {$IFDEF IOS}
       LaunchURL := TiOSOpenApplicationContext(AContext).URL;
  {$ENDIF}
     end;
  end;
  Result := True;
end;

When the event executes and your app is launched the LaunchURL string will get filled with the URL that the app was launched using. You can then parse the LaunchURL string to get the various parameters out of it. You’ll need IdURI and IdGlobal in your uses section for the below code. Here is the sample code to parse the URL:

var
Params: TStringList;
MyValue: String;
begin
  if LaunchURL<>'' then
    begin
      Params := TStringList.Create;
      try
        URI := TIdURI.Create(LaunchURL);
        try
          Params.Delimiter := '&';
          Params.StrictDelimiter := true;
          // Path = URI.Path
          Params.DelimitedText := URI.Params;
        finally
          URI.Free;
        end;

        for I := 0 to Params.Count -1 do
          begin
            Params.ValueFromIndex[I] := StringReplace(Params.ValueFromIndex[I], '+', ' ', [rfReplaceAll]);
            Params.ValueFromIndex[I] := TIdURI.URLDecode(Params.ValueFromIndex[I], IndyTextEncoding_UTF8());
          end;

	MyValue := Params.Values['myname'];
      finally
        Params.Free;
      end;
      LaunchURL := '';
    end;
end;

Check out the Delphi 10.2 Tokyo documentation for TApplicationEvent.OpenURL which shows you the other properties of TiOSOpenApplicationContext.

 

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts
DelphiDemoFiremonkeyLinuxOSXShowcaseWindows

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging

DelphiFiremonkeyShowcaseUtilityWindows

Unleashing Creativity With Song Writer AI: A Deep Dive

DelphiFiremonkeyShowcaseWindows

How To Build Stable Diffusion Text To Image Prompts

AndroidC++BuilderDelphiFiremonkeyIOSOSXWindows

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

1 Comment

Leave a Reply