如何在 OS X 中的两个应用程序之间共享 NSUserDefaults
我正在编写一个将提交到 Mac App Store 的应用程序。作为应用程序的一部分,有一个帮助程序/后台应用程序。我的主应用程序需要能够将持久设置写入 NSUserDefaults。我的帮助程序应用程序(可能在后台运行)需要收到这些更改的通知,然后能够读取新的 NSUserDefaults,以便它可以适当地配置自身。
我不太担心它的通知部分。我看到 NSUserDefaults 有一个名为 NSUserDefaultsDidChangeNotification 的通知,该通知在持久默认值更改时发送。如果这不起作用,我总是可以使用分布式通知。
我似乎无法弄清楚如何使主应用程序的默认设置可供后台/帮助应用程序访问。我已经摆弄了 NSUserDefaults 的以下方法,但我没有得到任何结果。
- (void)addSuiteNamed:(NSString *)suiteName
- (void)setPersistentDomain:(NSDictionary *)domain forName:(NSString *)domainName
有人可以向我展示主应用程序和有效的后台/帮助应用程序的代码吗?提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 addSuiteNamed: NSUserDefaults 的方法。
这仅适用于读取设置,不适用于写入。
You can use addSuiteNamed: method of NSUserDefaults.
This will work only for reading settings, not for writing.