Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Open another App via URL with Parameters


This question is answered.


Permlink Replies: 3 - Last Post: Nov 18, 2016 4:35 PM Last Post By: Remy Lebeau (Te...
Sebastian Brenner

Posts: 2
Registered: 9/26/16
Open another App via URL with Parameters  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 17, 2016 12:58 AM
Hi,

i program two apps for my iPad. I want to open the one app out of the other app.
Both apps have an URL and I open them with
SharedApplication.openURL(TNSURL.Wrap(TNSURL.OCClass.URLWithString(StrToNSSTR(PChar(String('APPNAME://'))))));


I read on this [page|http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html], that you can open an app with parameters like this example:
APPNAME://?token=123abct&registered=1

Now i want to know how to extract the parameters in the second app. The site gives me this Code but i don't know how to realize this in Delphi.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
        sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
  NSLog(@"URL scheme:%@", [url scheme]);
  NSLog(@"URL query: %@", [url query]);
 
  return YES;
}


Thanks for help!
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Open another App via URL with Parameters
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 17, 2016 11:35 AM   in response to: Sebastian Brenner in response to: Sebastian Brenner
Sebastian wrote:

SharedApplication.openURL(TNSURL.Wrap(TNSURL.OCClass.URLWithString(StrToNSSTR(PChar(String('APPNAME://'))))));

That is a bit long-winded. For one thing, the PChar type-cast is not necessary
since StrToNSSTR() takes a String as input. But more importantly, FireMonkey
has a wrapper function to create an NSURL from a String:

uses
  ..., Macapi.Helpers;
 
SharedApplication.openURL(StrToNSUrl('APPNAME://'));


Now i want to know how to extract the parameters in the second app.

Try using the RTL's ParamCount() and ParamStr() functions.

--
Remy Lebeau (TeamB)
Sebastian Brenner

Posts: 2
Registered: 9/26/16
Re: Open another App via URL with Parameters  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 17, 2016 10:50 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks for helping!

Try using the RTL's ParamCount() and ParamStr() functions.

I used these two functions but only get one parameter. The given params are
SharedApplication.openURL(StrToNSUrl('test2://?token=123abct&registered=1'));


The parameter i get is:
/var/containers/Bundle/Application/24C640AA-0302-49DD-894C-6C77FBF6B267/Test2.app/Test2

There is nothing like '123abct'. What am i doing wrong?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Open another App via URL with Parameters
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 18, 2016 4:28 PM   in response to: Sebastian Brenner in response to: Sebastian Brenner
Sebastian wrote:

I used these two functions but only get one parameter. The given
params are

SharedApplication.openURL(StrToNSUrl('test2://?token=123abct&registered=1'));


The parameter i get is:

/var/containers/Bundle/Application/24C640AA-0302-49DD-894C-6C77FBF6B267/Test2.app/Test2

That looks like the app name, which would make sense for ParamStr(0).

There is nothing like '123abct'. What am i doing wrong?

Nothing. Clearly ParamStr() is not hooked up for receiving URL parameters.
I did say "try it", not "this WILL work"! I'm not a mobile developer, or
a FireMonkey user, for that matter.

So, looking into this deeper, it turn out the application that is being launched
must use an application delegate to handle incoming URLs. Delphi XE8+
(actually even earlier, maybe XE5/6, but the openURL delegate was not
hooked up correctly until XE8) already handles this for you, providing two
different options to get the event:

1. use FireMonkey's messaging framework (http://docwiki.embarcadero.com/RADStudio/en/Sending_and_Receiving_Messages_Using_the_RTL)
to subscribe to the TApplicationEvent.OpenURL event (http://docwiki.embarcadero.com/Libraries/en/FMX.Platform.TApplicationEvent),
which will pass you a TiOSOpenApplicationContext containing the URL.

2. use TPlatformServices.SupportsPlatformService() to retreive FireMonkey's
IFMXApplicationEventService interface (http://docwiki.embarcadero.com/Libraries/en/FMX.Platform.IFMXApplicationEventService),
and then call its SetApplicationEventHandler() method to assign an event
hander that looks for TApplicationEvent.OpenURL events. It will
provide the same TiOSOpenApplicationContext with the URL.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02