NSUserDefaults 未在 IOS4 中加载

发布于 2024-09-30 14:59:13 字数 1658 浏览 5 评论 0原文

我有一个在 OS 3.x 下编写的应用程序 - 当时它运行良好。 自从为 OS 4.1 重建以来,它不再适用于加载应用程序设置。

我有一个与应用程序捆绑在一起的“root.plist”文件。一些默认设置已经通过编辑文件设置在其中,即主机名、超时等。新用户必须提供一些登录凭据,因此这些凭据留空。当应用程序首次启动时,它会检测到丢失的凭据并关闭,要求用户转到“设置”并输入凭据。 如果我打开设置,我会看到我所期望的;主机名、超时和其他默认值,如 root.plist 中所示。我输入用户名和密码,以及另一个 ID 代码。 当我重新启动应用程序时,我会获取刚刚输入的数据的值,以及 BOOL 协议开关 = YES(我没有更改)。尽管出现在设置中,其他值都返回零。

username   = [[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"];
password   = [[NSUserDefaults standardUserDefaults] stringForKey:@"password_preference"];
CRMID      =  [[[[NSUserDefaults standardUserDefaults] stringForKey:@"userID_preference"]uppercaseString] retain];
hostname   = [[NSUserDefaults standardUserDefaults] stringForKey:@"hostname_preference"];
protocolSwitch = [[NSUserDefaults standardUserDefaults] boolForKey:@"http_preference"];
timeout    = [[NSUserDefaults standardUserDefaults] doubleForKey:@"timeout_preference"];
portNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"port_preference"];
bccEmail   = [[NSUserDefaults standardUserDefaults] stringForKey:@"bcc_email_preference"];
locationOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"location_preference"];

我知道“同步”,但我实际上并没有在我的应用程序中更改这些值,所以我不知道这如何适用于此处。我还确保应用程序在重新启动之前没有在后台运行,没有什么区别。 在 OS4 之前它运行得很好,有人理解行为的变化吗?有办法解决吗? 干杯,

Sarge62。

设置结果肯定有问题。 当应用程序启动时将字典键转储到控制台后,我看到了以下内容:

“位置偏好”,
NSInterfaceStyle,
苹果语言,
“用户ID_偏好”,
苹果键盘扩展,
苹果语言环境,
苹果键盘,
NS语言,
“http_首选项”,
“密码_偏好”,
“名称_偏好”

plist 中只定义了五个键! 如果我进入设置并将主机名更改为 plist 默认值以外的名称,它将出现在上面的日志中。 设置屏幕中似乎有一个错误(?),它只注册从初始默认值更改的键(至少对于字符串对象,BOOLS 是可以的)。

I have an app that was written under OS 3.x - it worked fine back then.
Since rebuilding for OS 4.1 it no longer works with regard to loading the app settings.

I have a 'root.plist' file bundled with the app. Some of the default settings are already set in there by editing the file, i.e. hostname, timeout, etc. A new user must supply some login credentials so these are left blank. When the app starts for the first time, it detects the missing credentials and shuts down, asking the user to go to Settings and enter credentials.
If I open settings, I see what I expect; a hostname, timeout and other defaults as they were in root.plist. I enter the username and password, plus one other ID code.
When I restart the app, I get values back for the data I just keyed in, plus the BOOL protocolswitch = YES (which I did not change). The other values all return nil, despite appearing in settings.

username   = [[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"];
password   = [[NSUserDefaults standardUserDefaults] stringForKey:@"password_preference"];
CRMID      =  [[[[NSUserDefaults standardUserDefaults] stringForKey:@"userID_preference"]uppercaseString] retain];
hostname   = [[NSUserDefaults standardUserDefaults] stringForKey:@"hostname_preference"];
protocolSwitch = [[NSUserDefaults standardUserDefaults] boolForKey:@"http_preference"];
timeout    = [[NSUserDefaults standardUserDefaults] doubleForKey:@"timeout_preference"];
portNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"port_preference"];
bccEmail   = [[NSUserDefaults standardUserDefaults] stringForKey:@"bcc_email_preference"];
locationOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"location_preference"];

I know about "synchronize" but I'm not actually changing these values from within my app, so I don't see how that applies here. I've also made sure the app isn't running in background before restarting, makes no difference.
It worked perfectly before OS4, does anyone understand the change in behaviour? Any way to fix it?
Cheers,

Sarge62.

There is definitely something wrong with the Settings result.
Having dumped the dictionary keys to console when the app launches, I see this:

"location_preference",
NSInterfaceStyle,
AppleLanguages,
"userID_preference",
AppleKeyboardsExpanded,
AppleLocale,
AppleKeyboards,
NSLanguages,
"http_preference",
"password_preference",
"name_preference"

There are only five of the keys defined in the plist!
If I go to settings and change the hostname to something other than what was in the plist default, it will appear in the log above.
It seems that there is a bug(?) in the Settings screen that only registers keys that are changed from the initial default value (for string objects at least, BOOLS are OK).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-10-07 14:59:13

您是否注册了默认值?这通常是在应用程序委托的 + (void)initialize 类方法中完成的。

例如

+ (void)initialize {
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
[defaultValues setObject:@"defaultHostname" forKey:@"hostname"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}

Have you registered the defaults ? This is done typically in a + (void) initialize class method of the app delegate.

for example

+ (void)initialize {
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
[defaultValues setObject:@"defaultHostname" forKey:@"hostname"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文