【问题标题】:InAppSettings not populating on launchInAppSettings 在启动时未填充
【发布时间】:2012-06-26 11:50:09
【问题描述】:

我正在使用来自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 并从那里加载默认值,我的应用仍在获取默认值,但它们没有传递到应用内设置。

我认为第一次启动应用程序时应该是这样的......

【问题讨论】:

    标签: ios iphone settings.bundle inappsettingskit


    【解决方案1】:

    您想先注册您的NSUserDefaults。看看这个description

    【讨论】:

    • 目前我从正在加载的设置包中获得了我的默认值,上面的代码。查看您的链接,我是否还应该创建另一种加载默认值的方式?我的应用在第一次启动时从上面的代码中提取默认值,但它没有将其推送到应用内设置中。
    • 我已经尝试了第一个答案中的建议,创建了一个单独的 userDefaults.plist 并从那里加载默认值,我的应用程序仍然获得默认值,但它们没有传递到应用程序内设置。
    • 您可能需要添加[[NSUserDefaults standardUserDefaults] synchronize] 以确保新设置已同步到磁盘。我不明白您修改后的问题中的代码。我认为它应该以某种方式根据您的 Settings.plist 确定 NSUserDefaults。但它只是添加一个键而不是全部。我建议您在第一次开始时使用额外的 userDefaults.plist 来注册您的 NSUserDefaults。至少更容易!
    • 我有那个...我的问题是我的 if 语句中的代码从未被调用过。一旦我更改了我的 if 语句,它就开始像预期的那样工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2017-01-17
    • 1970-01-01
    • 2021-07-16
    • 2014-04-02
    • 2019-03-29
    相关资源
    最近更新 更多