Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

[iPhone] Receiving DefaultValue from .plist

Hello everyone,
my application has a Root.plist file like this:


...
<dict>
<key>Title</key>
<string>myTitle</string>
<key>Key</key>
<string>myMultiValueTest</string>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>DefaultValue</key>
<dict>
<key>boolValue</key>
<true/>
<key>stringValue</key>
<string>abc</string>
</dict>
<key>Values</key>
<array>
<dict>
<key>boolValue</key>
<true/>
<key>stringValue</key>
<string>foo</string>
</dict>
<dict>
<key>boolValue</key>
<false/>
<key>stringValue</key>
<string>bar</string>
</dict>
</array>
<key>Titles</key>
<array>
<string>title1</string>
<string>title2</string>
</array>
</dict>
...


(The real file is a little bit more complicated) In the program, I use something like

[[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"myMultiValueTest" valueForKey:@"stringValue"];

This works (returns "foo"/"bar") after one of the options has been selected in the Settings App.

If I understand the +Settings Application Schema Reference+ correctly, this should return "abc" by default, i.e. if the none of the options has been selected.
However, all I get is null. Any idea what's wrong here?

Mac Mini, Mac OS X (10.5.4)

Posted on Jan 7, 2009 1:13 AM

Reply
2 replies

Jan 7, 2009 2:28 AM in response to Pumbaa

Hi,

as far as i know the default-values given in that plist are used by the settings.app only in case there's currently no value in the user-defaults.

You need to call registerDefaults in your app with the same default-values to get a default-value returned.

Look at the sample-code "AppPrefs". Here the relevant part:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[window addSubview:[navigationController view]];

NSString *testValue = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];
if (testValue == nil)
{
// no default values have been set, create them here based on what's in our Settings bundle info
//
NSString *pathStr = [[NSBundle mainBundle] bundlePath];
NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:@"Settings.bundle"];
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"Root.plist"];
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"];
NSString *firstNameDefault;
NSString *lastNameDefault;
NSNumber *nameColorDefault;
NSNumber *backgroundColorDefault;

NSDictionary *prefItem;
for (prefItem in prefSpecifierArray)
{
NSString *keyValueStr = [prefItem objectForKey:@"Key"];
id defaultValue = [prefItem objectForKey:@"DefaultValue"];

if ([keyValueStr isEqualToString:kFirstNameKey])
{
firstNameDefault = defaultValue;
}
else if ([keyValueStr isEqualToString:kLastNameKey])
{
lastNameDefault = defaultValue;
}
else if ([keyValueStr isEqualToString:kNameColorKey])
{
nameColorDefault = defaultValue;
}
else if ([keyValueStr isEqualToString:kBackgroundColorKey])
{
backgroundColorDefault = defaultValue;
}
}
// since no default values have been set (i.e. no preferences file created), create it here
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
firstNameDefault, kFirstNameKey,
lastNameDefault, kLastNameKey,
[NSNumber numberWithInt:1], kNameColorKey,
[NSNumber numberWithInt:1], kBackgroundColorKey,
nil];

[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
[[NSUserDefaults standardUserDefaults] synchronize];
}

// we're ready to do, so lastly set the key preference values
firstName = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];
lastName = [[NSUserDefaults standardUserDefaults] stringForKey:kLastNameKey];
textColor = [[NSUserDefaults standardUserDefaults] integerForKey:kNameColorKey];
backgroundColor = [[NSUserDefaults standardUserDefaults] integerForKey:kBackgroundColorKey];
}

[iPhone] Receiving DefaultValue from .plist

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.