iphone : Settings.bundle returns null value

后端 未结 2 661
谎友^
谎友^ 2021-01-14 04:18

I was using xCode 3.2 and then moved to xCode 4.2 and getting some values from Settings.bundle ... it was working fine.

Mean while I need to edit some values in Sett

2条回答
  •  终归单人心
    2021-01-14 05:03

    I got the solution, just call the below code in applicationDidFinishLaunchingWithOptions to initialize User defaults. and it works

    Replace somePropertyYouExpect in first line with property you stored in User Defaults.

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"somePropertyYouExpect"])  {
    
        NSString  *mainBundlePath = [[NSBundle mainBundle] bundlePath];
        NSString  *settingsPropertyListPath = [mainBundlePath
                                               stringByAppendingPathComponent:@"Settings.bundle/Root.plist"];
    
        NSDictionary *settingsPropertyList = [NSDictionary 
                                              dictionaryWithContentsOfFile:settingsPropertyListPath];
    
        NSMutableArray      *preferenceArray = [settingsPropertyList objectForKey:@"PreferenceSpecifiers"];
        NSMutableDictionary *registerableDictionary = [NSMutableDictionary dictionary];
    
        for (int i = 0; i < [preferenceArray count]; i++)  { 
            NSString  *key = [[preferenceArray objectAtIndex:i] objectForKey:@"Key"];
    
            if (key)  {
                id  value = [[preferenceArray objectAtIndex:i] objectForKey:@"DefaultValue"];
                [registerableDictionary setObject:value forKey:key];
            }
        }
    
        [[NSUserDefaults standardUserDefaults] registerDefaults:registerableDictionary]; 
        [[NSUserDefaults standardUserDefaults] synchronize]; 
    } 
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题