我正在使用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并从那里加载默认值,我的应用程序仍然获得默认值,但它们没有传递到应用内设置。
我认为在第一次发布应用程序时应该看起来像这样......