Home > iPhone > iPhone Default User Settings Null?

iPhone Default User Settings Null?

I wanted to set default values for my application using a Settings.bundle and I ran into an interesting issue with iPhone SDK 2.2. If you don’t run the Settings application before your application runs for the first time, then the default settings are not set. It turns out that you’ll need to manually set them on the first time the application is run.

Setting defaults to the default value…

I’m not sure why there isn’t a function that can set the values for me, but after digging I found someone who ran into the same dilema. I don’t want to have multiple locations with default values (code and a Settings.bundle), so I found a programmatic way to set all the default values if they haven’t been set.

- (void)registerDefaultsFromSettingsBundle {
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    if(!settingsBundle) {
        NSLog(@"Could not find Settings.bundle");
        return;
    }
 
    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
 
    NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
    for(NSDictionary *prefSpecification in preferences) {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if(key) {
            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
        }
    }
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
    [defaultsToRegister release];
}

All you have to do is call the above function if one of your Standard User Defaults returns null. Make the call once your application finishes loading like so:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
	// Get the application user default values
	NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
	NSString *server = [user stringForKey:@"server_address"];
	if(!server) {
		// If the default value doesn't exist then we need to manually set them.
		[self registerDefaultsFromSettingsBundle];
		server = [[NSUserDefaults standardUserDefaults] stringForKey:@"server_address"];
	}
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

References:

Categories: iPhone Tags: ,
  1. Chris
    July 8th, 2009 at 06:24 | #1

    Thanks Paul!

  2. cc
    July 17th, 2009 at 18:48 | #2

    thanks a lot, saved me some time and works great.

  3. August 31st, 2009 at 14:47 | #3

    Thanks this is really helpful. One thing I had to change though is to check that the value for a default is not nil before setting it. Otherwise the app would crash if not all the defaults had non-nil values.

  4. September 16th, 2009 at 16:27 | #4

    You’re welcome!

    @SamiShaio I would try to avoid situations where it could be null, but that depends on what you’re doing. Keep your settings options simple for the Settings Bundle.

  5. November 6th, 2009 at 07:46 | #5

    Great post. My updated app had been rejected with no explanation as to why from Apple, found out it was due to this. I will be referencing your article on my own blog shortly.

  6. November 6th, 2009 at 09:49 | #6

    awesome! I’m glad to have helped you.

  7. Gene
    February 6th, 2010 at 19:12 | #7

    Thanks Paul, a great post. I found this post while searching for the answer to a related issue. Not only did your post help resolve my other issue, it alerted me to one I hadn’t yet considered. – Gene, RIT Alumni ‘80-’84

  8. February 7th, 2010 at 02:17 | #8

    Glad to hear back from an Alum!

  1. No trackbacks yet.