Start iOS App with arguments and access them within Unity

Hello,

I am developing an ios app.
I can start the app via my URL scheme very simply.
But how can I access the arguments within the URL?
My URL is like myApp://host/key=value

I got it from this site: http://www.yelp.com/developers/documentation/iphone
But I don’t know how to do that with Unity.

I hope you can help me.
Thanks in advance.

Best Regards,
Unity Nerd

Hello,

now I found it by myself and a bit of google!

You have to write an interface for the AppController to overwrite the function “didFinishLaunching” with “didFinishLaunchingWithOptions”.
I found that on google.
Then you have to tell unity the result via UnitySendMessage(“GameObject”, “Function”, result);

But this just working if the “Function” was written in CS, never succeded in JS. I don’t know why.
So you can access the arguments and in my case I just used String that I can parse as I need it.

Hope it helps others and don’t hesitate to ask for specific code explanation.

Best Regards,
Unity Nerd

You actually don’t even need to overwrite it.

Just add a category that implements the function, takes the custom argument, stores it into NSUserDefaults and sync and then call didFinishLaunching :slight_smile:

if you ask yourself why NSUserDefaults: this maps to PlayerPrefs inside of Unity and is persistent, so you don’t risk that the data could be lost anyway.

2 Likes

Hi,

I’m trying to do this, but with no success.

I have added that method to AppController.mm:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if (!url) {  return NO; }
    
    NSString *URLString = [url path];
    [[NSUserDefaults standardUserDefaults] setObject:URLString forKey:@"url"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return YES;
}

And that in one of my game object class:

string passedData = PlayerPrefs.GetString("url");
Debug.Log("PlayerPrefs > URL" + passedData);

But the value for key “url” is empty.

What am I missing?

Thanks!

Got it to work by adding the code into the didFinishLaunchingWithOptions method:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    
    NSArray *keyArray = [launchOptions allKeys];
	if ([launchOptions objectForKey:[keyArray objectAtIndex:0]]!=nil) {
		NSURL *url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
		NSString *urlString = [url absoluteString];

        [[NSUserDefaults standardUserDefaults] setObject:urlString forKey:@"url"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

....

}

@smallfly1

Dear Sir, I cannot express how much I owe you a beer. I WILL raise a glass to you tonight as this was the last thing for my project. You have answered a whole week of headaches for me.

Seriously… Thank you

The solutions here weren’t working for me. In UnityAppController.mm there is a new method that handles the open url

  • (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation

See this post regarding sending the url to unity.

This code mostly worked for me. I was sending a string as an argument (my flag was “automation”), so I had to change my code to this

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
  NSArray *keyArray = [[NSProcessInfo processInfo] arguments];
  for(id key in keyArray)
  {
  if (key!=nil && [key isEqualToString:@"automation"])
  {
  NSString *automation = key;
  [[NSUserDefaults standardUserDefaults] setObject:automation forKey:key];
  [[NSUserDefaults standardUserDefaults] synchronize];
  }
  }
...
}

Then on the Unity side I called this

string passedData = PlayerPrefs.GetString("automation");
            Debug.Log("PlayerPrefs > automation: " + passedData);
            if(passedData == "automation")
            {
                //start my automation
            }