UserDefaults not being saved in Simulator only

In Xcode 13, using the 14.5 and 15.0 simulators, when I use UserDefaults to save preferences for the user, the defaults seem to go away when the app quits (I am using the home button / app switcher / slide up gesture to quit the app in the simulator).

This problem does not occur when using my actual devices (iPad and iPhone).

[userDefaults synchronize] returns YES, however I am getting nil for the key I saved upon next launch.

Note: I am using NSKeyedArchiver archivedDataWithRootObject with requiresSecureCoding set to YES for some keys, however the first key is not using NSKeyedArchiver and is also returning nil.

  • I created a simplified project and tested with iOS 15.0 simulator (iPhone 8 Plus)/Xcode 13, but I could not reproduce the issue. There may be some problem in your code. Can you create a simplified project to reproduce the issue and show all the code of it?

  • Addendum: There was actually an issue with my code that I have fixed. However I still have users experiencing this problem sporadically when the app opens, and the only conclusion I can make is that the UserDefaults are either not-readable or have been erased. I have not personally been able to reproduce the issue but have heard from over a dozen users of my app.

Add a Comment

Replies

I have the same problem, but only on Apple Watch simulators... On iPhone Simulators is everything fine...

What was your problem?