I am working on an iPhone app but now there is also a counterpart watch app too. The user can login into the iPhone app. I want to get this login details like the access token into the watch app even if the iPhone is not reachable or iPhone app is not running. Once the user login to the iPhone app, I get the access token into the watchapp immediately with updateApplicationContext method. But how do I save this data in watch app so that if iPhone app is not even running then even I can have the access token of user in watch app?
Asked
Active
Viewed 233 times
-1
-
I think `NSKeyedArchiver` works with WatchOS. Did you already try it? – Adrian May 15 '20 at 08:15
-
I was wondering if I can use the UserDefaults in the watch app in the same way that it is used in iOS app. – cherry_4 May 15 '20 at 08:22
-
Yes you can do that. There is even a way to use a shared user defaults container between the iPhone app and the watch app to avoid data redundancy. – Adrian May 15 '20 at 09:12
-
@Adrian I will try this one. Thanks – cherry_4 May 15 '20 at 09:47
-
You're welcome. Please let me know if it's working for you. – Adrian May 15 '20 at 09:56
-
did you figure out for this your question? if yes could u share with me – Energy Jun 27 '21 at 03:01
1 Answers
0
The easiest way is to use a shared user defaults container.
1) Activate AppGroup for all your targets in the Capabilities tab.
2) Use your group's identifier for user defaults:
let userDefaults = UserDefaults(suiteName: "group.com.YourCompany.YourApp")

Adrian
- 352
- 1
- 11
-
This does not work in the new WatchOS versions(it worked in the WatchOS 1 or 2 only). WatchConnectivity has to be used for the purpose. – cherry_4 Jul 02 '20 at 10:06