Saved data lost after kill app using NSUserDefaults

ios, nsuserdefaults

Solution

I don't see a reason for that.

Have you checked in the Simulator directory, under Library/Caches there's the plist that saves your NSUserDefaults. Check if it's written correctly.

And please, don't use setInteger:TRUE. This is wrong on so many levels. Use setBOOL:YES instead. Or setInteger:1.

Problem

I use NSUserDefaults in my setting. App works well even I press home key let app into background, but if I kill the app, the data save in NSUserDefaults will lost. Here is my code. I have use synchronize. The First initialize: ``` if (![userDefaults integerForKey: kORFootageAirPlayModeKey]) { [userDefaults setInteger:TRUE forKey:kORFootageAirPlayModeKey]; } [userDefaults synchronize]; ``` Read value out in a viewController: ``` airPlayMode = [[NSUserDefaults standardUserDefaults]integerForKey:kORFootageAirPlayModeKey]; ``` Set it in a action: ``` - (IBAction)changeAirPlayStatus:(id)sender { if (sender) { airPlayMode = [sender tag]; [[NSUserDefaults standardUserDefaults] setInteger:airPlayMode forKey:kORFootageAirPlayModeKey]; [[NSUserDefaults standardUserDefaults] synchronize]; } ..... ``` }

Original source