Apple 是否将 NSUserDefaults 共享从 iOS 应用更改为 watchOS 应用

Did Apple change NSUserDefaults sharing from iOS app to watchOS app

我想问一下如何在 watchOS 应用程序上使用 NSUserDefaults。 它的数据是否与 iOS 应用程序 NSUserDefaults 的数据不同?

of 关于这个主题的 Whosebug 问题,他们都有相同的答案。也就是说,例如

Watch apps that shared data with their iOS apps using a shared group container must be redesigned to handle data differently. In watchOS 2, each process must manage its own copy of any shared data in the local container directory. For data that is actually shared and updated by both apps, this requires using the Watch Connectivity framework to move that data between them.

但是,所有引用的文字都从 the web page referred, see 中消失了。

相反,在当前 Apple Docs。有

Additionally, iOS automatically forwards a read-only copy of your iOS app’s preferences to Apple Watch. Your WatchKit extension can read those preferences using an NSUserDefaults object, but it cannot make changes directly to the defaults database.

我有两个问题:

非常感谢

要回答你的第一个问题,你从 Whosebug 和 Apple 引用的两个答案都是正确的。 Apple 将 iOS 应用程序的 NSUserDefaults 转发为 read-only 值,但手表有自己的 NSUserDefaults 作为其偏好。文档的主要内容是让手表应用程序远离使用 NSUserDefaults 的共享容器组(因为这是 WatchKit 应用程序的实现方式)。在 watchOS 中,Apple 添加了 WatchConnectivity,这是 iOS 和 watch 应用程序之间共享数据的标准。

要回答您的第二个问题,请依赖 NSUserDefaults,因为您通常会单独存储与每个应用相关的首选项,并使用 WatchConnectivity 在应用之间共享数据。