I'm working on a Mac OS X app that was developed for Mountain Lion. The app performs some setup tasks the first time it runs. It then sets a flag in [NSUserDefaults standardUserDefaults]
; on subsequent runs the app checks to see if that flag is set, and does not perform these first time setup tasks if it is set.
On Mountain Lion, I was able to delete the ~/Library/Preferences/bundleid.plist file to wipe out everything stored in NSUserDefaults by the app. However, on more recent versions of Mac OS X, when the app runs it's not even creating that file. I verified that it is saving data successfully to NSUserDefaults by inspecting values coming back from [[NSUserDefaults standardUserDefaults] objectForKey:@"foo"]
in the debugger.
Can anyone point me in the right direction regarding how to delete my app's settings that are stored in NSUserDefaults?
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. Changes are made synchronously within the process of your application.
Because NSUserDefaults stores all data in an unencrypted . plist file, a curious person could potentially view this data with minimal effort. That means that you should never store any type of sensitive data inside NSUserDefaults.
Storing Default Objects The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs.
This method is using a pthread_mutex_t to lock the access to the dictionary containing the values. So NSUserDefaults is thread safe.
Mavericks isn't released yet, so I'm ignoring that part of the question. You should be asking on the developer forums if you want a Mavericks-specific answer.
Regardless of OS X version, the right way to delete a defaults domain is with defaults delete bundleid
or its programmatic equivalent. ~/Library/Preferences
is an implementation detail. The plists contained therein do not always contain the latest information. Prior to Mountain Lion, defaults changes are buffered in individual applications until they synchronize
; in Mountain Lion and later, they are maintained in memory in cfprefsd
processes and flushed to disk lazily.
See the Core Foundation release notes for 10.8 for more information.
2016 update: A current guide to the state of NSUserDefaults is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With