Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 238
Bartosz Bialecki Avatar asked Mar 07 '14 16:03

Bartosz Bialecki


People also ask

Where are the NSUserDefaults values stored?

All the contents saved by NSUserDefaults is saved inside a plist file that can be found under Library -> Preferences -> $AppBundleId.

Where are UserDefaults stored Macos?

Navigate to AppData -> Library -> Preferences. The property list file (. plist) in this folder is the database file of the standard UserDefaults of your app.

Where is UserDefaults saved?

Storing Data in User Defaults The user's defaults database is stored on disk as a property list or plist. A property list or plist is an XML file. At runtime, the UserDefaults class keeps the contents of the property list in memory to improve performance.

How much Data can you store in NSUserDefaults?

It appears the limit is the maximum file size for iOS (logically), which is currently 4GB: https://discussions.apple.com/thread/1763096?tstart=0. The precise size of the data is circumscribed by the compiler types (NSData, NSString, etc.) or the files in your asset bundle.


Video Answer


2 Answers

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

Mac sandbox created but no NSUserDefaults plist

like image 54
Jay Avatar answered Sep 25 '22 11:09

Jay


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
like image 1
Henrik Avatar answered Sep 25 '22 11:09

Henrik