iOS 8.2 Settings.bundle default value

2019-02-26 17:43发布

问题:

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!

回答1:

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.



标签: xcode ios8.2