NSUserDefaultsController with defaults for another app

  • Greetings Cocoa-devs!

    My app ships as a System Preferences preference pane that contains a
    faceless agent app inside it's bundle.  The agent app provides all of
    the functionality for my app. The prefpane stores preferences in the
    plist file for my app (rather than that for the host System
    Preferences app).  I've been able to make this work by sending
    persistentDomainForName: to the shared instance of NSUserDefaults.

    Now i'm trying to use bindings in order to simplify displaying a
    collection of dictionaries in an NSTable.  I can see my preference
    data via an NSUserDefaultsController if i initialize it like this
    where defaultsController is actually an instance of NSObjectController:

    NSUserDefaults *defaultsInstance = [NSUserDefaults
    standardUserDefaults];
    [defaultsInstance addSuiteNamed:DOMAIN];
    NSUserDefaultsController *controller = [[NSUserDefaultsController
    alloc] initWithDefaults: defaultsInstance initialValues: nil];
    [defaultsController setContent:controller];

    My NSTable shows the correct data, but when i update the information
    it is persisted back to the com.apple.systempreferences.plist file,
    and not the plist file for my app.  Craig Hockenberry posted a similar
    question about 3.5 years ago (http://tinyurl.com/5upqnp) without
    receiving many responses and google has so far turned up very little.

    Is there a way for me to bind my preferences to my view that's hosted
    in another app (in this case System Preferences?).

    Thank you so much,
    Keith Alperin
  • On Aug 12, 2008, at 11:11 PM, Keith Alperin wrote:

    > My app ships as a System Preferences preference pane that contains a
    > faceless agent app inside it's bundle.  The agent app provides all
    > of the functionality for my app. The prefpane stores preferences in
    > the plist file for my app (rather than that for the host System
    > Preferences app).  I've been able to make this work by sending
    > persistentDomainForName: to the shared instance of NSUserDefaults.
    >
    > Now i'm trying to use bindings in order to simplify displaying a
    > collection of dictionaries in an NSTable.  I can see my preference
    > data via an NSUserDefaultsController if i initialize it like this
    > where defaultsController is actually an instance of
    > NSObjectController:
    >
    > NSUserDefaults *defaultsInstance = [NSUserDefaults
    > standardUserDefaults];
    > [defaultsInstance addSuiteNamed:DOMAIN];
    > NSUserDefaultsController *controller = [[NSUserDefaultsController
    > alloc] initWithDefaults: defaultsInstance initialValues: nil];
    > [defaultsController setContent:controller];
    >
    > My NSTable shows the correct data, but when i update the information
    > it is persisted back to the com.apple.systempreferences.plist file,
    > and not the plist file for my app.  Craig Hockenberry posted a
    > similar question about 3.5 years ago (http://tinyurl.com/5upqnp)
    > without receiving many responses and google has so far turned up
    > very little.
    >
    > Is there a way for me to bind my preferences to my view that's
    > hosted in another app (in this case System Preferences?).

    This page of the preference pane documentation says that
    NSUserDefaults isn't useful to preference panes, precisely because of
    the issues you're seeing:

    http://developer.apple.com/documentation/UserExperience/Conceptual/Preferen
    cePanes/Concepts/Managing.html#/

    /apple_ref/doc/uid/20000703-BABHJCCA

    Presumably, that extends to NSUserDefaultsController, too.

    It shouldn't be too hard, though, to create a model object of your own
    which exposes your preferences as its properties.  It would use the
    CFPreferences API as its backing store.  There would be more work
    required to support all of the features of NSUserDefaultsController,
    though -- things like the deferred application of changes, reverting,
    restoring factory settings, etc.

    Cheers,
    Ken