0

I've read the Apple doc about Preferences but this is still a little bit complex for me to understand. I have an application with a custom screen for setting the Preferences, and I'd like just the code to manage the read and write stuff.

Would you know a detailed tutorial (not writen years ago) or a project sample code somewhere I could read to understand ?

Oliver
  • 23,072
  • 33
  • 138
  • 230
  • Not sure which you're asking: do you want to set up a settings bundle like in the page you linked, or just store in-app preferences? If it's the latter then it's a much easier task. – Jordan Smith Mar 22 '11 at 12:41
  • @Jordan : I assume to manage the way Preferences are displayed. I just want to be able to read and write a preferences file. – Oliver Mar 22 '11 at 13:08

1 Answers1

6

You should use NSUserDefaults :

You set it like that:

       NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

then you can set new objects like that:

   [defaults setBool:YES forKey:@"bools"];
   [defaults setObject:[NSNumber numberWithInt:14] forKey:@"numbers"];
   [defaults setFloat:60.0 forKey:@"floats"];
   [defaults setObject:@"simple string" forKey:@"strings"];
   [defaults setObject:[NSDate date]  forKey:@"dates"];

when you need to read a value you can use :

   NSUInteger integerFromPrefs = [defaults integerForKey:@"integers"];
   BOOL boolFromPrefs = [defaults boolForKey:@"bools"];
       NSString *stringFromPrefs = [defaults objectForKey:@"bools"];
       etc...

and remember to synchronize your changes after each change:

   [defaults synchronize];

BTW

You can read and write to the NSUserDefaults from any view in your application.

Edit

To see all of the data in the NSUserDefaults you can use:

  NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

This will print all the keys and values stored in the plist.

GOOD LUCK

shannoga
  • 19,649
  • 20
  • 104
  • 169
  • @shani : do I need to write a plist file ? – Oliver Mar 22 '11 at 13:21
  • 1
    NO NSUserDefaults is a "build in" plist and you can use it without creating nothing. just follow the steps i posted and thats all actually. – shannoga Mar 22 '11 at 13:22
  • @shani : where can I find the written file (when running on the simulator, or the iPhone I don't have choice, when running a camera app for example) ? – Oliver Mar 22 '11 at 13:25
  • Edited my question. and did not understand your last one. – shannoga Mar 22 '11 at 13:32
  • @shani : I mean, there is a file written by those calls so I'd like to be able to see it and edit it, to inject it again in the app. Sometimes, such for camera applications, you can't use the simulator but need to test on the phone. In both cases, I wonder where is the created preferences file. – Oliver Mar 22 '11 at 13:51
  • 1
    You can look here: http://stackoverflow.com/questions/1676938/easy-way-to-see-saved-nsuserdefaults – shannoga Mar 22 '11 at 13:53
  • @shani : how can I know if a key exists in the file ? For example when I read a 1.0 prefs with a 2.0 app where some features where not present in 1.0, to be able to set a default value. The default value returned by "xxxxforKey" when key is not found doesn't help to know : it can be a user value, or a missing key. – Oliver Mar 22 '11 at 13:59