如何在 OS X 中的两个应用程序之间共享 NSUserDefaults

发布于 2024-12-16 11:32:19 字数 571 浏览 1 评论 0原文

我正在编写一个将提交到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2024-12-23 11:32:19

您可以使用 addSuiteNamed: NSUserDefaults 的方法。

- (void)addSuiteNamed:(NSString *)suiteName  

这仅适用于读取设置,不适用于写入。

You can use addSuiteNamed: method of NSUserDefaults.

- (void)addSuiteNamed:(NSString *)suiteName  

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文