Question

Possible Duplicate:
Multivalue type settings bundle fields alway return null

I have a settings bundle in my iOS app which generates the following table in the settings app

In my app I attempt to generate the same table using NSUserDefaults and end up with this

My code to retrieve the values goes is:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
ds->use_cache = [userDefaults boolForKey:@"use_cache"];
ds->high_bitrate = [userDefaults boolForKey:@"high_bitrate"];

and

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[[cell detailTextLabel] setText:[defaults stringForKey:@"version"]];

The stringForKey with the key "version" always returns a null value. I can't seem to get my app to recognise the default parameters I have defined in the settings bundle.

Here is my settings bundle

Was it helpful?

Solution

The default values for the settings should be set using the registerDefaults: method. This sets up an NSRegistrationDomain from which they will be available. An example for this is below. Thanks to jrturton & Matthias Bauch for pointing out the problem with my first answer that was previously referring to http://greghaygood.com/2009/03/09/updating-nsuserdefaults-from-settingsbundle.

NSDictionary *appDefaults = [ NSDictionary
                              dictionaryWithObject:@"default_version_value" forKey:@"version" ];

[ [ NSUserDefaults standardUserDefaults ] registerDefaults:appDefaults ];

NSUserDefaults *userDefaults = [ NSUserDefaults standardUserDefaults ];

Also note these default values need to be set every time the application starts as stated in the documentation for registerDefaults:.

The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top