我正在使用来自http://www.inappsettingskit.com/的应用内设置。这真的很棒,但我不知道如何让设置在启动时自动加载到应用内设置中。
当我第一次启动我的应用程序时,所有应用程序内的多值单元格都是空白的。
我想知道的是第一次启动应用程序时如何加载它们?
默认值是从设置包中加载的,但不会传递到应用内设置...目前这是我的代码...
-applicationDidFinishLaunching:
//It is here that we set the defaults
NSString *textValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"title_key"];
//If the first value is nil, then we know that the defaults are not set.
if(textValue == nil)
{
//We set the default values from the settings bundle.
//Get the bundle path
NSString *bPath = [[NSBundle mainBundle] bundlePath];
NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];
NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];
NSDictionary *item;
NSString *title_Key;
NSString *detail_Key;
NSString *sort_Key;
for(item in preferencesArray)
{
//Get the key of the item.
NSString *keyValue = [item objectForKey:@"Key"];
//Get the default value specified in the plist file.
id defaultValue = [item objectForKey:@"DefaultValue"];
if([keyValue isEqualToString:@"title_key"]) title_Key = defaultValue;
if([keyValue isEqualToString:@"detail_key"]) detail_Key = defaultValue;
if([keyValue isEqualToString:@"sort_key"]) sort_Key = defaultValue;
}
//Now that we have all the default values.
//We will create it here.
NSDictionary *appPrerfs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], @"title_key",
[NSNumber numberWithInt:4], @"detail_key",
[NSNumber numberWithInt:2], @"sort_key",
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appPrerfs];
[[NSUserDefaults standardUserDefaults] synchronize];
}
我也尝试了第一个答案中的建议,创建一个单独的 userDefaults.plist 并从那里加载默认值,我的应用程序仍然获得默认值,但它们没有传递到应用程序内设置。
我认为它应该在第一次启动应用程序时看起来像这样......