Search code examples
iphoneobjective-ccocoa-touchios-simulatornsuserdefaults

NSUserDefaults not being saved in iOS Simulator?


My [NSUserDefaults standardUserDefaults] are not being saved. Everytime I close the app and start it again, it reverts back. I don't have my iOS device handy so I am not sure if this will happen on device, but none of the other apps are doing this in simulator which leads me to believe there's something wrong with my code. I don't know what part of code I should include here, it's just simple add/modify keys in NSUserDefaults and as I said it runs fine during the app, but not after i restart it...

I know I can call synchronize but Apple advices against it and says I should only call it if it's a must... so.

What could be going wrong?


Solution

  • Your process is possibly terminated improperly so that NSUserDefaults do not have a chance to be stored. See also this and mostly this.

    The suggestion in the second post I link to is to call synchronize in applicationDidEnterBackground:

    Keep also in mind that terminating your app by stopping it in Xcode most often does not save user defaults.