I have an iPhone and an Apple Watch paired together, and there are some values stored in the user defaults of the phone. I want my Apple Watch to be able to retrieve that stored information somehow and bring it back to the Watch. What is the best method of achieving this with Swift?
-
https://developer.apple.com/documentation/watchconnectivity/wcsession – Paulw11 Jul 01 '17 at 00:25
1 Answers
Since watchOS2
, you don't have any built in function for communicating between the iOS and watchOS app other than the WatchConnectivity
framework. Due to the fact that Watch apps are no longer considered App Extensions, they don't have access to AppGroups
and hence to UserDefaults
on the iPhone.
For syncing UserDefaults
, the updateApplicationContext(_:) function seems to be the best solution. You can send a dictionary of data with this function (the data you just saved to UserDefaults
on the iPhone) and the system tries to make sure that the data is received by the time your app is displayed to the user. If the function is called several times before the app would be visible to the user (run in the foreground), the system overwrites the previous data, so the Watch app only receives the most recent data to display.

- 51,403
- 9
- 85
- 116
-
Can we dat store directly to watch app using Userdefaults? Because I tried for storing and retrieve data on watch app its working on simulator for watch app. For eg I have stored image data and retrieved on watch app itself. Whether its valid to store data in watch app ? – The iCoder Nov 26 '18 at 13:48
-
1You can store data in `UserDefaults` from the watch app directly, however, you cannot sync data between the iOS and watchOS apps using `UserDefaults`. – Dávid Pásztor Nov 26 '18 at 14:07
-
Thanks for quick update. Is there any data limit or it depend upon memory available by OS? – The iCoder Nov 26 '18 at 14:43
-
@TheiCoder as explained in [this](https://stackoverflow.com/questions/7510123/is-there-any-limit-in-storing-values-in-nsuserdefaults) Q&A, no, but you shouldn't use `UserDefaults` as your main persistent storage if you're storing complex data, use a real database solution, such as `CoreData` or `Realm` in that case. – Dávid Pásztor Nov 26 '18 at 15:06
-
Ok I will update my app implementation accordingly and most probably we will use Core data in this case. Thanks for your guidance. Appreciated..!!! – The iCoder Nov 26 '18 at 15:32