How to store iOS startup custom scheme data in NSUserDefaults to read from Unity

My app is developed using Unity3d (2018) and c#. The app is then build in Cloud Build for iOS. Now, I want to use a custom scheme to launch my iOS app and provide some extra information:

some-custom-scheme://some-parameter=some-value

I’ve read in the Unity forum that the best way to do this is to create a plugin in Unity and hook into the iOS startup pipeline as described in the forum post like this:

-(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:rage:“url”];
[[NSUserDefaults standardUserDefaults] synchronize];
}

return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

Within the app, I then simply check the PlayerPrefs like this:

var url = PlayerPrefs.GetString(“url”);

The url value however is empty. Iterating through the keys and alerting them in the app give two results: UIApplicationLaunchOptionsKey and UIApplicationLaunchOptionsSourceApplicationKey. Both irrelevant according the Apple documentation which state that the data should be in the UIApplicationLaunchOptionsAnnotationKey key.

I’ve also read this SO question telling the poster to use the openURL method.

I feel that I’m close but not there yet. What am I doing wrong and how can it be fixed? Which method is better? My approach using didFinishLaunchingWithOptions or the openURL method?

Any help is greatly appreciated!

@markachten88 Please edit your post to use Code Tags

I don’t think that NSUserDefaults maps to PlayerPrefs.

You might want to send a message from XCode to Unity using UnitySendMessage, which will send a message to a gameobject in Unity.

More details can be found here: Unity - Manual: Native plug-ins for iOS

Thanks for the help! But you’re wrong. NSUserDefaults do map to PlayerPrefs:

We now do the communication with UnitySendMessage.

@markacthen88 Unity’s documentation doesn’t mention anything about NSUserDefaults mapping to PlayerPrefs.

All it says is “On iOS, PlayerPrefs are stored in /Library/Preferences/[bundle identifier].plist.” However NSUserDefaults apparently map to the same plist file (iPhone: Where NSUserDefaults get stored? - Stack Overflow)

That link you gave says “If I’m not wrong…” and to try it… If I understood your original post correctly, the mapping is not there as one would think. If you’ve tried NSUserDefaults and cannot read it from PlayerPrefs, then there’s something off.

Glad that UnitySendMessage worked for you.

Mmm, I guess you’re right. I read it in multiple places so assuming that it was correct. But the again… assumptions remain the mother of all f…