It 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;
Thanks for the article, it helps a lot.
One note, .plist code snippet looks corrupted, I believe it has to something like:
CFBundleURLTypes
CFBundleURLName
com.my.bundle-id
CFBundleURLSchemes
myappurlschemename