Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting bundle default value won't set

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 (using the DefaultValue property) to YES but any time the application launches on a device or the iOS simulator all values go NO. It worked well only on the first launch.

Settings Bundle PLIST

I am retrieving the defaults with this code (am I doing something wrong here?):

NSUserDefaults *localeDefaults = [NSUserDefaults standardUserDefaults];
BOOL ENWORDS = [localeDefaults boolForKey:@"localetime"];
like image 244
iOS.Lover Avatar asked Feb 07 '12 18:02

iOS.Lover


4 Answers

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];
like image 101
Matthias Bauch Avatar answered Sep 17 '22 08:09

Matthias Bauch


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!)

like image 33
deanWombourne Avatar answered Sep 19 '22 08:09

deanWombourne


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

like image 41
Mc.Lover Avatar answered Sep 17 '22 08:09

Mc.Lover


- (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
}
like image 41
mr.pppoe Avatar answered Sep 18 '22 08:09

mr.pppoe