My NSUserDefaults work fine while the app is running or minimized. But if I kill the app and restart it, the object returned is null. Code:
colorDefault = [NSUserDefaults standardUserDefaults];
[colorDefault setObject:myColor2 forKey:@"fieldKey"];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:myColor2, @"fieldKey", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
//[[NSUserDefaults standardUserDefaults] synchronize];
[colorDefault synchronize];
I call [[NSUserDefaults standardUserDefaults] synchronize] in my applicationDidEnterBackground and in the applicationWillTerminate, with no success. I NSLogged both of those methods, and when I kill the app, I notice only the DidEnterBackground is being called..
Thanks for ANY help!
Mmmm… I think your code should be simpler.
This is what I do to write NSUserDefault and to retrieve it:
setObject is not the only option: setBool, setFloat, setInteger and on the other side boolForKey, floatForKey, integerForKey are good as well.
Do not forget to set
Settings.bundleandaKeyin your project:Hope this helps…