An important piece missing


After reading this chapter I think there is a piece missing: how to load bundle settings into user defaults.

I tried a couple solutions and this link gave me the answer:

Just do following in BNRAppDelegate.m:

+ (void)initialize {



void registerDefaultsFromSettingsBundle() {
    NSLog(@"Registering default values from Settings.bundle");
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    [defs synchronize];

    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

        NSLog(@"Could not find Settings.bundle");

    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)
            // check if value readable in userDefaults
            id currentObject = [defs objectForKey:key];
            if (currentObject == nil)
                // not readable: set value from Settings.bundle
                id objectToSet = [prefSpecification objectForKey:@"DefaultValue"];

                if (objectToSet) {

                    [defaultsToRegister setObject:objectToSet forKey:key];
                    NSLog(@"Setting object %@ for key %@", objectToSet, key);

                // already readable: don't touch
                NSLog(@"Key %@ is readable (value: %@), nothing written to defaults.", key, currentObject);

    [defs registerDefaults:defaultsToRegister];
    [defs synchronize];

Your defaults in the bundle setting will be loaded into user defaults.


A very big help, thanks. I’m surprised this issue wasn’t written up earlier.


Hmm, I’m not sure I understand the purpose of the above code. The app works as expected for me, without this additional code. When I first launch the app, “Coffee Cup” is the default item name, so adding a new item automatically populates the name field with Coffee Cup. But then, if I exit the app and go into Settings and modify the default item name to something else like “Car”, and then go back into Homepwner and add a new item, the new item’s default item name is Car. Isn’t that what we expect? Doesn’t that mean that the user defaults were updated from the Settings bundle?

If I’m missing something, I would really appreciate an explanation of why the above code that fremontdad posted is necessary.