Setting bundle default value won't set

前端 未结 5 965
小蘑菇
小蘑菇 2021-01-04 02:29

I have an iOS application with a settings.bundle that handles various settings for my application with Switch toggle. I set default values in my root.plist file

5条回答
  •  时光说笑
    2021-01-04 02:53

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        //< Register Defaults
        NSString *settingsBundlePath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
        NSBundle *settingsBundle = [NSBundle bundleWithPath:settingsBundlePath];
        NSString *rootPlistPath = [settingsBundle pathForResource:@"Root" ofType:@"plist"];
        NSDictionary *settingsDict = [[NSDictionary alloc] initWithContentsOfFile:rootPlistPath];
        NSArray *settingsItems = [settingsDict objectForKey:@"PreferenceSpecifiers"];
        NSMutableDictionary *defaultDict = [NSMutableDictionary new];
        for (NSDictionary *itemDict in settingsItems) {
            if ([itemDict objectForKey:@"DefaultValue"]) {
                [defaultDict setObject:[itemDict objectForKey:@"DefaultValue"] forKey:[itemDict objectForKey:@"Key"]];
            }
        }
        [[NSUserDefaults standardUserDefaults] registerDefaults:defaultDict];
        //< Following Code
    }
    

提交回复
热议问题