2011. 9. 23. 18:04
IPhone
Setting Bundle 설정하기
설정하기 : 파일추가>리소스>Settings.bundle
iOS5에서 Settings.bundle 추가후 해야할 사항(iOS5 Xcode의 현재 버그임)
1) Click on the Settings.Bundle file, go over to the utilities window,and look in the File Inspector.
2) Using the drop-down, change the file type to 'Application Bundle'
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];
NSLog(@"name before is %@", name);
// Note: this will not work for boolean values as noted by bpapa below.
// If you use booleans, you should use objectForKey above and check for null
if(!name) {
[self registerDefaultsFromSettingsBundle];
name = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];
}
NSLog(@"name after is %@", name);
}
- (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];
}
출처 : http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the-s/510329#510329