Bartosz Bialecki
Bartosz Bialecki

Reputation: 4451

OS X: NSUserDefaults not saved

I have a sandboxed app for OSX and I saved some data in NSUserDefaults, everything was fine until I deleted the plist file from the ~Library/Preferences/ directory.

I thought the app should recreat it but it did not. When I debug I see that

[[NSUserDefaults standardUserDefaults] synchronize] method returns YES

and data are saved in the class but when I restart the app user defaults is empty.

Even if I copy those plist file from backup to the Preferences directory, the app does not see it.

Of course the plist should be saved in the Containers/my app bundle id/Data/Library/Preferences and when I copy the plist file from backup to that directory, the app can see it, but why that file is not recreated when I deleted it?

Does anybody know why is that?

Upvotes: 16

Views: 2778

Answers (2)

Henrik
Henrik

Reputation: 4034

I had this problem, which is caused by deleting the files manually. What you can do instead is to use the defaults terminal command. This way I was able to reset the defaults without breaking things. Replace "bundle-identifier" with your bundle id.

To view the current values:

defaults read bundle-identifier

To remove stored values:

defaults delete bundle-identifier

Upvotes: 1

Jay
Jay

Reputation: 6638

Check this question - might be related to caching or prefs location when running in the sandbox:

Mac sandbox created but no NSUserDefaults plist

Upvotes: 2

Related Questions