Did Apple change NSUserDefaults sharing from iOS app to watchOS app

旧时模样 提交于 2020-01-03 04:44:08

问题


I want to ask about how to use NSUserDefaults on watchOS app. Is its data different from iOS app's NSUserDefaults's data?

There are a lot of stackoverflow questions about this topic and all of them have same answers. That said, for example

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.

However, all the quoted text disappeared from the web page referred, see this accepted answer.

Instead, in current Apple Docs. There is

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.

I have 2 questions:

  • Which one is correct: all of stackoverflow questions' answers I mentioned above, or, the Apple Docs

  • 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 be deprecated in near future?

Many thanks


回答1:


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.

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.



来源:https://stackoverflow.com/questions/42204781/did-apple-change-nsuserdefaults-sharing-from-ios-app-to-watchos-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!