Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - reading Setting.bundle returns wrong values

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

like image 932
Duck Avatar asked Mar 30 '11 19:03

Duck


1 Answers

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;
}
like image 76
0x8badf00d Avatar answered Nov 10 '22 06:11

0x8badf00d