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?

+2


source to share


2 answers


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 .



+4


source


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.



+3


source







All Articles