Pitfall in using default values in settings bundle

If you are implementing application settings using iPhone’s Application Preferences, then you will want to take note of this pitfall.

Referring to the schema reference for PSTextFieldSpecifier, you would notice that there is a DefaultValue key. You might thought that this key is to return a default value, when the preference is not set.

In terms of code, you might thought that myvalue would return the default value.

NSString *myvalue = [[NSUserDefaults standardUserDefaults] stringForKey:@"mykey"]

But no, that is NOT the case. myvalue will always return nil, until you go to the Application Preferences page. In order to read a default value correctly, you will have to do more.

Refer to AppPrefs Sample Code on how that can be done. The code is copied here for reference.

	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;
 
		NSDictionary *prefItem;
		for (prefItem in prefSpecifierArray)
		{
			NSString *keyValueStr = [prefItem objectForKey:@"Key"];
			id defaultValue = [prefItem objectForKey:@"DefaultValue"];
 
			if ([keyValueStr isEqualToString:kFirstNameKey])
			{
				firstNameDefault = defaultValue;
			}
			// Handle for other preferences you might have
			// ...
		}
 
		// since no default values have been set (i.e. no preferences file created), create it here		
		NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
										firstNameDefault, kFirstNameKey,
										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];

If you like this, share it:
  • Facebook
  • Twitter
  • Google Bookmarks
  • Digg
  • del.icio.us
  • email
  • RSS
  • DZone
  • http://topsy.com/just2us.com/2010/08/pitfall-in-using-default-values-in-settings-bundle/?utm_source=pingback&utm_campaign=L2 Tweets that mention Just2us » Blog Archive » Pitfall in using default values in settings bundle — Topsy.com

    [...] This post was mentioned on Twitter by Junda Ong, txeet. txeet said: Pitfall in using default values in settings bundle – If you are implementing application settings using iPhone’s App… http://ow.ly/18nf6J [...]

  • Ka

    Thanks for the help…

  • Nick Pears

    this has been driving me NUTS! Many thanks! BUT… What is Apple playing at?!?! Surely a deafult is something that gets automatically used in the absence of a user making a decision?

  • vicky

    very nice… thanks for this great help.. :)