10
votes

I'm trying to create settings bundle for custom keyboard extension on ios 8. I've added settings bundle to keyboard extension target. Then when the keyboard view is loaded I'm trying to access the setting bundle in following way.

BOOL autoCapitalizationEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"autoCapitalizationEnabled"];

the result is always 0, it seems that program can't access to setting bundle file.

Does anyone has the issue before with extensions settings bundle? By the way , the thing is not related with the default values of settings bundle, because every time I disable and enable this property on settings before keyboard launched to force the default value.

1

1 Answers

0
votes

Although you had set the DefaultValue on each item on Setting.bundle, it will not be written on you NSUserDefaults, so when you retrieve from NSUserDefaults, it will return a null (in this case, a "0", or NO, whatever).

Follow the steps below to setup your settings on NSUserDefaults on first launch.

First you would check if you already check and set the default values:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if ([userDefaults boolForKey:@"PreferenceFirstSetup"]) {

    //return if you already done the job once.
    return;
}

And if not, let's setup the default values ...

[userDefaults setBool:YES forKey:@"PreferenceFirstSetup"];

NSString *settingsPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Settings.bundle"];
NSString *rootPath = [settingsPath stringByAppendingPathComponent:@"Root.plist"];

NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:rootPath];
NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];

for (NSDictionary *item in preferencesArray) {

    NSString *key = [item objectForKey:@"Key"];
    id defaultValue = [item objectForKey:@"DefaultValue"];

    if (key && defaultValue)
        [userDefaults setObject:defaultValue forKey:key];
}

//Save on disk
[userDefaults synchronize];

Now, you should be able to access the default values. Hope it helps!