文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Settings.bundle的DefaultValue不起作用?(NSUserDefaults)

Settings.bundle的DefaultValue不起作用?(NSUserDefaults)

时间:2010-12-29  来源:Elf Sundae

在程序的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{}中检测设置的项值,如果为nil就全部设为DefaultValue的值:

 

// [YourApp]AppDelegate.m
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {

NSString
*name = [[NSUserDefaults standardUserDefaults]
stringForKey:
@"name_preference"];
if(!name) {
// 加载默认配置
[self performSelector:@selector(registerDefaultsFromSettingsBundle)];
}

[window addSubview:viewController.view];
[window makeKeyAndVisible];

return YES;
}
// 获取默认设置
- (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];
}

 

 

Download DefaultSettingsDemo

 

Ref:http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the-s

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载