Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS with NSUserdefaults on iphone

Tags:

iphone

I have the following code in my ApplicationDelegate. My deployment target is 3.0 and upwards, however I get a EXC_BAD_ACCESS when I launch the app with the following code on my iPhone with 3.1.3, however on the simulator which has 4.2 it runs fine.

This problem is now SOLVED. The code below is working, and it got a EXC_BAD_ACCESS due to a NSURLConnection in my code. I won't delete my code if anyone else get this problem. Thanks for your help.

UPDATE: Plist file:

<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
    <dict>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
        <key>Title</key>
        <string>Check for report on start?</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSToggleSwitchSpecifier</string>
        <key>Title</key>
        <string>Autocheck reports?</string>
        <key>Key</key>
        <string>FAutoUpdatePrefKey</string>
        <key>DefaultValue</key>
        <true/>
    </dict>
</array>

Applicationdelegate

+ (void)initialize {

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *pListPath = [path stringByAppendingPathComponent:@"Settings.bundle/Root.plist"];

NSDictionary *pList = [NSDictionary dictionaryWithContentsOfFile:pListPath];

NSMutableArray *prefsArray = [pList objectForKey:@"PreferenceSpecifiers"];
NSMutableDictionary *regDictionary = [NSMutableDictionary dictionary];

for (NSDictionary *dict in prefsArray) {
    NSString *key = [dict objectForKey:@"Key"];
    if(key) {
        id value = [dict objectForKey:@"DefaultValue"];
        [regDictionary setObject:value forKey:key];
    }
}
[[NSUserDefaults standardUserDefaults] registerDefaults:regDictionary];
}
like image 741
LuckyLuke Avatar asked Dec 04 '25 06:12

LuckyLuke


1 Answers

The registerDefaults: method has been available since iOS 2.0 (See Documentation), so that shouldn't be the issue. The problem probably stems from the regDictionary object and how you are filling it.

Take a look at this previous question. The main answer has an excellent method to do what you are trying to accomplish. In fact the code is nearly identical except for how the settings bundle is accessed: Can you make the settings in Settings.bundle default even if you don't open the Settings App

Here is the code you want (Stolen directly from the linked question above from the answer by PCheese)

- (void)registerDefaultsFromSettingsBundle {
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    if(!settingsBundle) {
        NSLog(@"Could not find Settings.bundle");
        return;
    }

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];

    NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
    for(NSDictionary *prefSpecification in preferences) {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if(key) {
            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
        }
    }

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
    [defaultsToRegister release];
}

UPDATE: Now that I see your pList, Change your PSToggleSwitchSpecifier's default value from <true/> to <string>YES</string>

like image 128
theChrisKent Avatar answered Dec 06 '25 00:12

theChrisKent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!