即使您不打开设置应用程序,您能否将 Settings.bundle 中的设置设为默认值 [英] Can you make the settings in Settings.bundle default even if you don't open the Settings App

查看:20
本文介绍了即使您不打开设置应用程序,您能否将 Settings.bundle 中的设置设为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 settings.bundle 的 iPhone 应用程序,用于处理我的应用程序的各种设置.我可以在我的 root.plist 文件中设置默认值(使用 DefaultValue 属性),但这些仅在用户第一次打开设置应用程序时使用.有没有办法在您的应用程序安装时写出这些值?我知道我可以编写代码来检查我的应用程序的第一次启动,然后将它们写出来,但它们在两个不同的地方.

I have an iPhone application with a settings.bundle that handles various settings for my application. I can set default values in my root.plist file (using the DefaultValue property), but these only get used the first time the user opens the settings app. Is there any way to get these values written out when your application installs? I know I can just write code that checks for the first launch of my app and then write them out, but then they are in two different places.

这里以我的 root.plist 中的条目为例:

Here is an entry from my root.plist as an example:

<dict>
    <key>Type</key>
    <string>PSToggleSwitchSpecifier</string>
    <key>Title</key>
    <string>Open To Top Location</string>
    <key>Key</key>
    <string>open_top_location</string>
    <key>DefaultValue</key>
    <string>YES</string>
    <key>TrueValue</key>
    <string>YES</string>
    <key>FalseValue</key>
    <string>NO</string>
</dict>

最终结果应该是,如果我要求open_to_top_location",我会得到是",而不是直到用户第一次打开设置"应用时它才出现.

The end result should be that if I ask for 'open_to_top_location' I get a YES, instead of it not being there at all until the first time the user opens the Settings app.

有什么想法吗?

推荐答案

如果我理解正确,您希望避免将默认值指定两次(一次作为 Settings.bundle/Root.plist 文件中的DefaultValue"键,并且一旦在您的应用初始化代码中),您就不必让它们保持同步.

If I understood you correctly, you want to avoid having default values specified twice (once as "DefaultValue" keys in your Settings.bundle/Root.plist file, and once in your app initialization code) so you do not have to keep them in sync.

由于 Settings.bundle 存储在应用程序包本身中,您只需读取那里给出的默认值.我整理了一些示例代码,这些代码查看设置包并读取那里每个键的默认值.请注意,这不会写出默认密钥;如果它们不存在,您需要在每次发布时阅读并注册它们(随意更改).我只做了一些粗略的测试,所以请确保它适用于所有情况.

Since Settings.bundle is stored within the app bundle itself, you can just read the default values given there. I put together some sample code that looks at the Settings bundle and reads the default values for every key there. Note that this does not write out the default keys; if they don't exist, you'll need to read and register them at every launch (feel free to change this). I've only done some cursory tests, so make sure it works for you in all cases.

- (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 && [[prefSpecification allKeys] containsObject:@"DefaultValue"]) {
            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
        }
    }

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
    [defaultsToRegister release];
}

这篇关于即使您不打开设置应用程序,您能否将 Settings.bundle 中的设置设为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆