Register default settings from the Settings.bundle plist file?

前端 未结 4 1420
闹比i
闹比i 2020-12-21 06:20

I\'m adding a Settings.bundle file to my iOS application. It\'s very minimal with one or two settings. The plist file in the Settings.bundle specifies the defaults, to the S

4条回答
  •  囚心锁ツ
    2020-12-21 06:58

    Wanted to add this as a comment to Andy's answer, but Stack Overflow didn't let me (too long for a comment).
    If you're using ObjC here's your version:

    -(void)registerDefaults
    {
        NSString* pathString = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Settings.bundle"];
        NSString* rootPath = [pathString stringByAppendingPathComponent:@"Root.plist"];
        NSDictionary* settingsDict = [NSDictionary dictionaryWithContentsOfFile:rootPath];
        NSArray* prefSpecifiers = settingsDict[@"PreferenceSpecifiers"] ;
    
        NSMutableDictionary* defaults = [NSMutableDictionary dictionary];
    
        for (NSDictionary* item in prefSpecifiers) {
            NSString* theKey = item[@"Key"];
            NSObject* defaultValue = item[@"DefaultValue"];
    
            if (defaultValue && theKey) {
                defaults[theKey] = defaultValue;
            }
    
        }
    
        if (defaults.count > 0) {
            [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
        }
    
    }
    

提交回复
热议问题