Search code examples
macospreferencesosx-lion

How can I write "all user" preferences in OS X Lion?


I'm working on an application which stores some preferences globally, for all users. It does so by passing kCFPreferencesAnyUser in as the userName parameter of CFPreferencesSetValue.

Prior to OS X version 10.7 (Lion), this worked fine. However, when this code runs on Lion, the call fails, even when the application is run from an Admin account. The documentation for CFPreferencesSetValue states:

Note that you can only save preferences for “Any User” if you have Admin privileges.

...and I see from searching around that the privileges required changed between Snow Leopard and Lion (from admin to root). It also seems that I could continue using similar code by following Apple's SMJobBless sample code...

...to securely install a helper tool that performs a privileged operation...

I note however that the documentation for kCFPreferencesAnyUser itself states:

This domain is currently unsupported.

Is using a helper tool the correct way to go about creating all-user preferences? Or is there some other, supported method I should be using?


Solution

  • This is actually a very good question, and there does not seem to be a particularly good answer.

    SMJobBless has the problem that you need to code-sign the tool with the same signature as the parent application. Unfortunately, this is System Preferences and hence only Apple can now produce the necessary code signing for the helper tool.

    Given this, I think that there are three options:

    • use an older authorisation API to run a helper tool (eg AuthorizationExecuteWithPrivileges)
    • use a standalone app to write the preferences
    • roll-your-own preference API (used either from a prefpane plug-in or not)

    The worst thing about all of this is that the CFPreference APIs appear to be failing completely silently - it is just that nothing actually happens when setting a global preference value.

    EDIT

    Sorry. I think that I was jumping ahead a bit. Yes if you are doing this from your own add then the SMJobBless code will do the trick. However, it is a pain to have to code a helper app and some form of IPC just to read and write shared preference values - it may be easier to roll your own preference system using a shared file if your needs are simple enough.