3

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?

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

Many thanks

Sang
  • 4,049
  • 3
  • 37
  • 47

1 Answers1

3

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.

lostAtSeaJoshua
  • 1,695
  • 2
  • 21
  • 34
  • this will be different question. To change/update iOS' `NSUserDefaults` from Watch Kit app. I am going to use `sendMessage:replyHandler:errorHandler:`, wait for `replyHandler:` response that `iOS` updated its own `NSUserDefaults`, after that make use of most recent `NSUserDefaults` data on the watch. Is `sendMessage:replyHandler:errorHandler:` appropriate in this situation. Many thanks – Sang Feb 14 '17 at 20:33
  • @tranvansang Yeah that is one way to do it. You can also use `transferUserInfo(_:)` https://developer.apple.com/reference/watchconnectivity/wcsession/1615671-transferuserinfo if the update on the iOS side can happen in the background. Also as far as your flow if the watch is updating something to the iOS app do not wait to read `NSUserDefaults` again. Just send your update to the iOS app and just update the watch appropriately without reading the defaults again. The watch should act as its own app. Send the message and act upon the update. Hope that helps. – lostAtSeaJoshua Feb 14 '17 at 21:02
  • thanks for replying. `transferUserInfo` can not make sure data is updated on the phone. Subsequent `NSUserDefaults` read may return unexpected not up to date data. Btw, i think there is a trade-off of choosing transfer method – Sang Feb 14 '17 at 21:31