iOS 8.2 Settings.bundle默认值 [英] iOS 8.2 Settings.bundle default value

查看:157
本文介绍了iOS 8.2 Settings.bundle默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Settings.bundle文件的应用程序. 我向特定字段添加了默认值,但是每当我想在iOS 8.2的应用程序中阅读此默认值时,都会返回"nil". 但是,当我在iOS 7.1(基本上是iOS 8.2以下的每个版本)上执行此操作时,我会获得正确的默认值.

有人知道iOS 8.2中是否有更改导致此问题,以及如何解决此问题?我已经删除了该应用并重新构建. 也许要注意一点,我是使用Xcode 5构建的.但是,如果我使用的是Xcode 5或Xcode 6,那并没有什么区别.

代码: 我如何检索默认值:

id settings = [NSUserDefaults standardUserDefaults];
[settings objectForKey:@"defaultValueUrl"];

Settings.bundle: http://i62.tinypic.com/10nrmhd.png

谢谢!

解决方案

在用户实际进入设置"并进行更改之前,设置"捆绑中的默认值不会写入到应用的NSUserDefaults中.情况一直如此.不确定为什么以前为您工作"?也许您从未从设备上卸载过该应用程序,并且保留了旧设置?

在代码中应该做的是在启动时注册默认设置. (是的,这与您的设置"捆绑包中发生的事情有些重复.就是这样.)

[[NUSUserDefaults standardDefaults] registerDefaults:
@{ @"defaultValueURL": @"http://example.com" }];

现在,无论是否带有设置"捆绑包,此默认键都将始终具有正确的默认值.

阅读此文章以了解更多信息.

I have an application with a Settings.bundle file. I added a default value to a specific field, but whenever I want to read this in the application on iOS 8.2, I get 'nil' back. However when I do this on iOS 7.1 (basically every version below iOS 8.2), I get the correct default value.

Does anyone know if there has been changed something in iOS 8.2 that is causing this and how to fix this? I've already deleted the app and built again. Maybe a small note, I build with Xcode 5. But that doesn't make any difference if I'm using Xcode 5 or Xcode 6. Tested this.

Code: How I retrieve the default value:

id settings = [NSUserDefaults standardUserDefaults];
[settings objectForKey:@"defaultValueUrl"];

Settings.bundle: http://i62.tinypic.com/10nrmhd.png

Thanks!

解决方案

The default values in your Settings bundle are not written to your app’s NSUserDefaults until the user actually goes to Settings and changes them. This has always been the case. Not sure why it was "working" for you before—maybe you never uninstalled the app from device and it kept the old settings around?

What you should do in your code, is to register default settings at startup. (Yes, this is somewhat duplicating what’s going on in your Settings bundle. That’s life.)

[[NUSUserDefaults standardDefaults] registerDefaults:
@{ @"defaultValueURL": @"http://example.com" }];

Now, this defaults key will always have the correct default value, with or without Settings bundle.

Read this post for more info.

这篇关于iOS 8.2 Settings.bundle默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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