How to set initial values for NSUserDefault Keys?
Asked Answered
P

4

43

I want to set some initial values for my NSUserDefault keys so that the first run of the app has some reasonable initial settings. I thought I ran across a simple way to do this in the app bundle .plist, but now I can't find it. Any ideas?

Perigee answered 12/12, 2011 at 12:1 Comment(1)
Check this : jayprakashdubey.blogspot.in/2014/07/nsuserdefault-usage.htmlGallard
T
71

You should use the registerDefaults method of NSUserDefaults. Prepare a plist file in your bundle that contains the default preferences and then use that plist to register the defaults.

NSString *defaultPrefsFile = [[NSBundle mainBundle] pathForResource:@"defaultPrefs" ofType:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfFile:defaultPrefsFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultPreferences];

You have to execute this code on every launch of your app. It will add these values to a separate domain in the user defaults hierarchy. Whenever your app's user defaults don't provide a value for a certain key, NSUserDefaults will fall back to this domain and retrieve the value from there.

Theriault answered 12/12, 2011 at 12:50 Comment(5)
It looks like this registers the defaults to a volatile memory location. I don't want it to go back to defaults very often at all. This is mostly for 1st launch. Will the user-set values be preserved, or will they occasionally be over-written by this?Perigee
What do you mean by "volatile memory location"? You have to execute this code on every launch of your app. It will add these values to a separate domain in the user defaults hierarchy. Whenever your app's user defaults don't provide a value for a certain key, NSUserDefaults will fall back to this domain and retrieve the value from there.Theriault
The User Defaults Programming Guide lists the NSRegistrationDomain as having a 'volatile' state, as opposed to 'persistent'. So, the user default settings themselves are persistent and the defaults are volatile, and have to be reloaded on every app launch. It just seems odd to me that the 'fall back' value is less persistent than the user setting.Perigee
I agree it seems a little odd but it works just fine and does make sense when you think about it.Theriault
@OleBegemann you should move your first comment, or part of it anyway, up into your answer. That was the first time I understood what registerDefaults actually did!Tactics
A
5

If you have many default values, let use ola's answer, otherwise this is good for a few params

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults boolForKey:USERDEFAULT_IS_INITIALIZED]) { 
    [defaults setBool:YES forKey:USERDEFAULT_IS_INITIALIZED];

    // Set initial values 
    ...

    [defaults synchronize];
}
Axil answered 8/5, 2014 at 4:32 Comment(0)
T
0
if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:@"initialValuesHaveBeenWritten"])
{
    [[NSUserDefaults standardUserDefaults] setValue:obj1 forKey:key1];
    [[NSUserDefaults standardUserDefaults] setValue:obj2 forKey:key2];


    [[NSUserDefaults standardUserDefaults] setValue:obj1 forKey:@"initialValuesHaveBeenWritten"];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

NB: Not tested, done from memory

Turdine answered 12/12, 2011 at 12:5 Comment(2)
This really isn't the correct way to to it. Works, but it's not what Apple suggests. Use -registerDefaults as described in the answer by @ole, or add them directly to the NSRegistrationDomain domain.Eme
I agree. I didn't know about registerDefaults when I wrote this answer but I use it now.Turdine
A
-1
-(void) loadDef
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    _removeAd=[userDefaults boolForKey:SAVE_AD_STATUS];

    NSString* strDefSetting=[userDefaults stringForKey:SAVE_STATUS_ADSETTING];
    if(strDefSetting==nil
       ||[strDefSetting isEqualToString:@""]
       )
    {
        strDefSetting=@"0.5";
    }

    _floatAdmob=strDefSetting.floatValue;//0.5;
}
Apollonian answered 29/8, 2014 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.