8

I have created a Settings.bundle that came with a Root.plist file and a localization directory en.lproj.

I have edited Root.plist and added several settings I want to have for my app.

When I delete the app from iPhone and install it and run the first time, all settings I read return wrong values. For example:

highQualityFlag = [[[NSUserDefaults standardUserDefaults] stringForKey:@"qualityFlag"] boolValue];

the flag comes as NO, even if the setting default is YES.

If I change something on the settings and run again, all subsequent runs give me the correct values (??)

How do I solve that?

thanks

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Duck
  • 34,902
  • 47
  • 248
  • 470

1 Answers1

24

Try this:

- (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];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    [self registerDefaultsFromSettingsBundle];

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [window makeKeyAndVisible];
    return YES;
}
John Sibly
  • 22,782
  • 7
  • 63
  • 80
0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
  • YESSSSSSSSSSSSSSSSSSSS !!!!!!!!!!!!!!!!!!!!!!!!!! Just one little question: do I need to add [self registerDefaultsFromSettingsBundle]; to applicationDidBecomeActive or applicationWillEnterForeground ? – Duck Mar 30 '11 at 20:26
  • Follow Up Question: I posted this question: http://stackoverflow.com/questions/8766979/nsuserdefaults-settings-bundle-plist where I basically want to have the user enter a url value to be used in the app. As of now the user can enter it and its saved, but its not read back when the app is launched. Rather the default value hardcoded into the plist is what's read. Could someone point out what Im doing wrong please. Why is the app still reading the default placeholder value? – marciokoko Jan 09 '12 at 23:09
  • @marciokoko did you eventually get a solution for your problem. Am facing the same ! – pnizzle Jan 14 '13 at 01:06
  • @marciokoko I have instead used the suggested solution and am reading the value from NSUserDefaults instead – pnizzle Jan 14 '13 at 01:10
  • is it possible to change the fields(like textfield, label)title of settings page programmatically. – iPhone Guy Mar 12 '14 at 12:49