问题描述
我想问一下如何在watchOS应用程序上使用NSUserDefaults
.它的数据与iOS应用程序NSUserDefaults
的数据不同吗?
I want to ask about how to use NSUserDefaults
on the watchOS app.Is its data different from the iOS app's NSUserDefaults
's data?
那里 都是 of 个stackoverflow问题,所有问题都有相同的答案.就是这样,例如
There are a lot of stackoverflow questions about this topic and all of them have same answers. That said, for example
However, all the quoted text disappeared from the web page referred, see this accepted answer.
相反,在当前的 Apple Docs .有
我有2个问题:
-
其中一个是正确的:我上面提到的所有StackOverflow问题的答案,或者Apple Docs
Which one is correct: all of StackOverflow questions' answers I mentioned above, or, the Apple Docs
iOS使用哪种机制将NSUserDefaults对象转发到watchOS应用?依靠它进行未来开发是否可靠?数据的最新程度如何?可以在不久的将来不推荐使用此功能吗?
Which mechanism iOS use to forward NSUserDefaults object to watchOS app? Is it reliable to be relied on for future development and how recent the data is up to date? Can this feature be deprecated in the near future?
非常感谢
推荐答案
要回答第一个问题,您从StackOverflow和Apple引用的答案都是正确的. Apple将iOS应用程序的NSUserDefaults
转发为只读值,但手表具有自己的NSUserDefaults
作为其首选项.该文档的主要内容是使监视应用程序脱离使用NSUserDefaults
的共享容器组(因为这是实现WatchKit
应用程序的方式). Apple在watchOS
中添加了WatchConnectivity
,这是在iOS和Watch应用之间共享数据的标准.
To answer your first question, both answers you quoted from StackOverflow and Apple are correct. Apple forwards the iOS app's NSUserDefaults
as read-only values, but the watch has it's own NSUserDefaults
for its preferences. The main takeaway from the documentation is for watch apps to move away from shared container groups that use NSUserDefaults
, (as this was how WatchKit
apps were implemented). In watchOS
, Apple has added WatchConnectivity
which is the standard for sharing data between the iOS and watch apps.
要回答第二个问题,请像往常一样依靠NSUserDefaults
分别存储与每个应用程序相关的首选项,并使用WatchConnectivity
在应用程序之间共享数据.
To answer your second question, rely on NSUserDefaults
as you would normally to store preferences related to each app separately and use WatchConnectivity
for sharing data between apps.
这篇关于苹果是否将NSUserDefaults共享从iOS应用更改为watchOS应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!