Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to initialize iPhone application defaults

I just started using the preferences pane to let users customize some settings in my iOS app, and it was pretty easy to create the Settings.bundle and access the information from it. My problem is that I read in the Apple docs that the settings should be programatically initialized:

It is recommended that you register any default preference values programmatically at launch time in addition to including them in your settings bundle property lists. For newly installed applications, default preference values from the application’s settings bundle are not set until the Settings application runs. This means that if the user runs your application before running Settings, the default values specified in your settings bundle will not be available. Setting such values programmatically at launch time ensures that your application always has appropriate values. To register default values programmatically, use the registerDefaults: method of the NSUserDefaults class.

Where in the app is this initialization done, and how can I be sure that I'm not overwriting a user-supplied value? Is this handled in some method of the App Delegate?

like image 781
Bill Shiff Avatar asked Dec 16 '22 17:12

Bill Shiff


1 Answers

You should register your defaults before you try to access a value stored in your NSUserDefaults. You could do it in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.
Registering your defaults is fast, so there is no need to optimize this. Just do it at the launch of the app.

I store my userdefaults in a plist and register the content of this list, like this:

NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DefaultUserDefaults" ofType:@"plist"]];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

If you register your defaults like this you don't have to worry that you overwrite user supplied values. NSUserdefaults uses "domains" where it stores it's values. If you register your defaults they are stored in the registration domain. If a User stores a value those values are stored in the application domain.

If you try to get a value from NSUserdefaults it looks if the value is present in the application domain, and if it's not there it takes the value from the registration domain.


Edit:

plist editor

you would access those values (or better, the values that are stored in your nsuserdefaults, and those as a fallback if there are no user provided values) like this:

NSInteger minutesToWait = [[NSUserDefaults standardUserDefaults] integerForKey:@"MinutesToWait";
NSString *urlString = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultURLStr"];

The plist is just another representation of a NSDictionary with keys and values. The key is the same key you use to access the userdefaults, and the value is your defaultvalue. Pretty straight forward.
It doesn't matter how you create the dictionary. You can do it in code as well.

like image 114
Matthias Bauch Avatar answered Dec 19 '22 06:12

Matthias Bauch