NSUserDefaults Mystery On Mac OS X

While working on my Mac app, I often faced the need to delete the NSUserDefaults file manually. The NSUserDefaults of a mac application are stored in a .plist file whose location depends on whether the application is sandboxed or not.

  • For sandboxed application, the .plist file is located at /Users/John/Library/Containers/<bundle-identifier>/Data/Library/Preferences
  • For non-sandboxed application, the .plist file is located at /Users/John/Library/Preferences

When deleting the .plist file, I faced a problem. Even after deleting the file, the application still seemed to be picking up values from it at runtime. A little bit of exploring led me to the fact that a process named cfprefsd retains a link to the .plist file even after it’s moved to trash and hence the deleted values still get served to the app.

I found that emptying the trash alone isn’t enough. The cfprefsd process must also be killed using the terminal command:

killall kill cfprefsd

Executing the above command delivers the behaviour we expect. Values are read from the new .plist file. Hope this comes handy to Mac OS X developers out there. Good Luck And Good Life!!!

 

Advertisements

3 thoughts on “NSUserDefaults Mystery On Mac OS X

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s