Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does NSUserDefaults store data on Mac OS X?

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?

like image 641
Greg Avatar asked Oct 07 '13 20:10

Greg


People also ask

Where NSUserDefaults data is saved?

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.

Is NSUserDefaults secure?

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.

What types can you store natively in NSUserDefaults?

Storing Default Objects The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs.

Is NSUserDefaults thread safe?

This method is using a pthread_mutex_t to lock the access to the dictionary containing the values. So NSUserDefaults is thread safe.


1 Answers

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.

like image 150
Nicholas Riley Avatar answered Sep 30 '22 17:09

Nicholas Riley