Carter
Carter

Reputation: 4758

How can I share NSUserDefaults between two applications in OS X

I'm writing an application that will be submitted to the Mac App Store. As part of the application there is a helper/background application. My main application needs to be able to write persistent settings to NSUserDefaults. My helper application, which is presumably running in the background, needs to be notified of those changes and then be able to read the new NSUserDefaults so that it can configure itself appropriately.

The notification portion of it I'm not too worried about. I see that NSUserDefaults has a notification named NSUserDefaultsDidChangeNotification that is sent when persistent defaults are changed. If that doesn't work there are always distributed notifications that I can use.

What I can't seem to figure out is how to make the defaults from the main application become accessible to the background/helper application. I've fiddled around with the following methods of NSUserDefaults but I'm not getting anywhere.

 - (void)addSuiteNamed:(NSString *)suiteName
 - (void)setPersistentDomain:(NSDictionary *)domain forName:(NSString *)domainName

Could somebody please show me code for both the main application and the background/helper application that works? Thanks in advance.

Upvotes: 0

Views: 1538

Answers (1)

Parag Bafna
Parag Bafna

Reputation: 22930

You can use addSuiteNamed: method of NSUserDefaults.

- (void)addSuiteNamed:(NSString *)suiteName  

This will work only for reading settings, not for writing.

Upvotes: 1

Related Questions