NSUserDefaults issues on MacOSX Mavericks explained

I’ve recently updated to MacOSX Mavericks and have run into some issues with our Mac App’s User Defaults,

it seemed that for some reason, the defaults stored in

~/Library/Preferences/<com.example.myapp>.plist are somehow being "ignored"

if you changed settings in there by hand or even delete the whole plist file, the app seems to still remember those settings, and magically the file will

appear again !

 

So how did this happen ? there doesn’t seem to be enough information about it, so I dug in a little bit and here’s my findings !

 

Apps have their own “Containers” in mavericks, storing their different settings in there.

Using NSUserDefaults, your app will look into the Container first, then in our good ‘ol

~/Library/Preferences/com.example.myapp.plist

OSX will  also the move settings in the container to

~/Library/Preferences/com.example.myapp.plist

if it’s not found.

Using your terminal, cd into

~/Library/Containers/com.example.myapp/Data/Library/Preferences

and then

ls -l

the contents, you’ll find lots of symlinks to apple’s app settings and in between, your app’s

.plist file,which won’t be a symlink, that’s where you should be editing your prefs.

TipI usually don’t recommend editing plist files manually, I’d rather use a tool called

defaults

simply fire up a terminal, and try this:

defaults read com.example.myapp

Sweet, huh ?!, that’s just scratching the surface ! using this tool you can read, edit or create any defaults you want, think “NSUserDefaults” for the terminal !

Why?, well consider this, if you weren’t modifying the defaults by hand, and using this tool instead, you would never had any issues with Mavericks.

If you have apps that play around with the defaults on the filesystem, use the 

defaults

tool instead, it’ll never break.

let me know your findings/thoughts in the comments !

 

TipIf you need to quickly reset your app’s state, run this in the terminal:

defaults delete com.myapp.* && rm -rf ~/Library/Preferences/com.myapp.*

2 comments

  1. bramstolk   •  

    I have a different issue since upgrading OSX.
    Storing my defaults used to work just fine.
    And the synchronize method returns no error.
    Yet:

    $ defaults read “com.steenriver.LittleCrane”
    2014-02-07 10:38:35.943 defaults[67495:507]
    Domain com.steenriver.LittleCrane does not exist

    I’m stomped. My old app, on older OSX, worked fine.
    Where are domains defined?

    Can I still use NSDefaults standardUserDefaults ?

    • Berg   •     Author

      hmmm that’s interesting, what happens when you use the NSUSerDefaults from your code ? is it giving you `Nil` results ?
      I think your case might be your old code isn’t creating defaults, that’s why you’re not seeing any using terminal, another trick is to set the defaults using terminal, and see if your app will be able to read them out !

      if you want you could also email me a code snippet of what you’re doing, will gladly take a look

Leave a Reply

Your email address will not be published. Required fields are marked *