How to get an update NSUserDefault on iOS 4?

限于喜欢 提交于 2019-12-03 12:24:07

I had the same problem as you, and got around it by calling

[[NSUserDefaults standardUserDefaults] synchronize];

in applicationDidBecomeActive.

For some reason the [NSUserDefaults standardUserDefaults] object you can access in your app isn't synchronized with the actual plist files backing it when an app becomes active again after having been suspended. Calling the synchronize method forces a synchronization.

You need to listen for settings changes. Best way to do it is to set up a listener wherever you need it:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingsChanged) name:NSUserDefaultsDidChangeNotification object:nil];

The only thing you need to make sure of is that you don't change any settings in your listener, or you get infinite recursion without a little more logic being added in.

I'd stay away from synchronizing because in my experience I found I had to run it twice. But that's accidental behavior. A listener will notify you when the new settings have been re-read from the store.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!