IPhone and NSUserDefaults
In my view WillLoad: method I am currently doing something along these lines:
- (void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ( [defaults boolForKey:@"enabled_preference"] ) {
...
} else {
...
}
[super viewWillAppear:animated];
}
If I build and run the application before opening the preference pane (built using the regular Settings.bundle) then bool seems to be NO (or rather nil) and not the default YES. However, if I open the Settings app and look at the app's preference pane before opening the app, everything works as expected.
I am assuming that the app settings are not initialized and I have to initialize them with the default (if not already set) in the app debit. Can someone confirm this? Or am I missing something else blindingly obvious here?
source to share
You must provide default values ββin your code using -registerDefaults:
. This is usually done using a method +initialize
for any class that uses settings. See Using NSUserDefaults .
source to share
Using the initialize method works, but I like fooobar.com/questions/99820 / ... which has code to read the default values ββfrom the package and use them to initialize the default values. This way you don't have to hardcode the defaults in your code, they are in the plist where they belong.
source to share