Setting bundle default value won't set

前端 未结 5 968
小蘑菇
小蘑菇 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:51

    The problem is the type of default Value must be boolean not string ;) delete this value and add a another default Value property again hope this helps

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-01-04 02:55

    This blog post might help : http://greghaygood.com/2009/03/09/updating-nsuserdefaults-from-settingsbundle

    tl;dr - until the user opens the settings page then the defaults aren't copied into your app. This is the expected behavior by Apple.

    Personally, I think this is terrible. It means that you will have to set your defaults in code just in case the user starts your app without going to the settings page first (which will be true for about 99% of use cases!)

    0 讨论(0)
  • 2021-01-04 03:03

    You can check whether the value has been set by getting objectForKey and checking whether it is nil.

       NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
       id dataExists = [userDefaults objectForKey:@"light_switch"];
       BOOL lightSwitch;
       if (dataExists != nil) {
          lightSwitch = [userDefaults boolForKey:@"light_switch"];
           NSLog(@"light_switch is %d", validateCertificates);
       } else {
          lightSwitch = YES; // default value
          NSLog(@"light_switch not set, default value is %d", validateCertificates);
       }
    
    0 讨论(0)
  • 2021-01-04 03:11

    The Default Value is used by Settings.app for display purposes only. If you don't change the value in the settings app nothing is saved to NSUserDefaults.

    You have to register the default values yourself. Use something like this in application:didFinishLaunchingWithOptions::

    NSDictionary *userDefaultsDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [NSNumber numberWithBool:YES], @"localetime",
                                          nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsDefaults];
    
    0 讨论(0)
提交回复
热议问题