Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by markachten88, May 13, 2019.

  1. markachten88

    markachten88

    Joined:
    Apr 24, 2015
    Posts:
    31
    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:mad:"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!
     
  2. xenonmiii

    xenonmiii

    Joined:
    Aug 2, 2010
    Posts:
    147
  3. markachten88

    markachten88

    Joined:
    Apr 24, 2015
    Posts:
    31
    Thanks for the help! But you're wrong. NSUserDefaults do map to PlayerPrefs:
    https://answers.unity.com/questions/850573/how-to-convert-ios-game-to-unity-and-use-the-same.html
    We now do the communication with UnitySendMessage.
     
  4. xenonmiii

    xenonmiii

    Joined:
    Aug 2, 2010
    Posts:
    147
    @markacthen88 Unity's documentation doesn't mention anything about NSUserDefaults mapping to PlayerPrefs.
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

    All it says is "On iOS, PlayerPrefs are stored in /Library/Preferences/[bundle identifier].plist." However NSUserDefaults apparently map to the same plist file (https://stackoverflow.com/questions/6662679/iphone-where-nsuserdefaults-get-stored)

    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.
     
  5. markachten88

    markachten88

    Joined:
    Apr 24, 2015
    Posts:
    31
    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...