下面是我在iOS 7中使用的代碼,主要基於上面的Mike Weller的代碼。在你的代碼
- (void)registerDefaultsFromSettingsBundleWithPlist:(NSString *)plist {
NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
if(!settingsBundle) {
NSLog(@"Could not find Settings.bundle");
return;
}
NSString *bundle = [NSString stringWithFormat:@"%@.plist",plist];
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:bundle]];
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];
}
然後調用它的每一個設置文件正在使用(嵌套設置),從一些地方早期像didFinishLaunchingWithOptions:
將這個方法在你AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//register default settings into NSUserDefaults
@try {
[self registerDefaultsFromSettingsBundleWithPlist:@"Root"];
[self registerDefaultsFromSettingsBundleWithPlist:@"Chat"];
[self registerDefaultsFromSettingsBundleWithPlist:@"IVR"];
[self registerDefaultsFromSettingsBundleWithPlist:@"Video"];
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
NSLog(@"Try adding the Default Value field to each preference item in the Settings.bundle plist files.");
}
@finally {
}
...
可能重複.bundle默認即使你不打開設置應用程序](http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you -dont-open-the) – 2012-05-08 11:38:23
'可能',我的意思是'確切'。 – 2012-05-08 11:45:39