Setting bundle default value won't set

Mc.Lover picture Mc.Lover · Feb 7, 2012 · Viewed 14.7k times · Source

I have an iOS application with a settings.bundle that handles various settings for my application with Switch toggle. I set default values in my root.plist file (using the DefaultValue property) to YES but any time the application launches on a device or the iOS simulator all values go NO. It worked well only on the first launch.

Settings Bundle PLIST

I am retrieving the defaults with this code (am I doing something wrong here?):

NSUserDefaults *localeDefaults = [NSUserDefaults standardUserDefaults];
BOOL ENWORDS = [localeDefaults boolForKey:@"localetime"];

Answer

Matthias Bauch picture Matthias Bauch · Feb 7, 2012

The Default Value is used by Settings.app for display purposes only. If you don't change the value in the settings app nothing is saved to NSUserDefaults.

You have to register the default values yourself. Use something like this in application:didFinishLaunchingWithOptions::

NSDictionary *userDefaultsDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithBool:YES], @"localetime",
                                      nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsDefaults];