Something strange happens to NSUserDefaults when loading an application

I have an application that uses several different user defaults that need to be used as soon as the application loads. One of them is responsible for zooming in map view for the location of users.

The problem I am facing is that when I load the user default values, it seems that they are zero until I changed them using the settings app.

What's going on here? I am using iOS 5, not sure if this affected him. This is just very annoying because my application relies on data availability at startup.

NOTE. It works absolutely fine after I changed / reinstalled the values ​​in the settings view.

+4
source share
1 answer

Make sure your call to synchronize after setting the value:

 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"someKey"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

Change response to comments:

From the documentation for -registerDefaults: ::

The contents of the registration domain are not written to disk; you need to call this method every time your application starts . You can put the plist file in the application resource directory and call registerDefaults: with the content you read in this file.

So, in -applicationDidFinishLaunching: you can register your default Root.plist values:

 [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Root" ofType:@"plist"]]]; 
+7
source

All Articles