0

I've a problem, with my app.. I try to follow some guide like Passing data to Apple Watch app , but I'm not sure it will fit for my case. I've some label with text on my iOS app, then I want to show this text on my watchOS app label, and I don't know which is the best way to pass this text and keep it synchronized with the iOS app.. Thanks a lot for your help!

MettDich
  • 17
  • 1
  • 5

3 Answers3

1

For watchOS1, Since Watch App is included as an extension in your Host App. So, you can use App Group to share data between your Host App and your App Extension.

Refer to https://stackoverflow.com/a/44654185/5716829 for more on using App Groups.

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • Since the introduction of `watchOS2`, Watch app's are not simply App Extensions and hence can't access data in the `App Group`. See [this answer](https://stackoverflow.com/a/30854052/4667835). Please update your answer to reflect that your solution only works for `watchOS1`. – Dávid Pásztor Jun 21 '17 at 10:35
0

Since watchOS2, you don't have any built in function for communicating between the iOS and watchOS app than the WatchConnectivity framework.

From the information provided in your question, the updateApplicationContext(_:) function seems to be the best solution for your problem. You can send a dictionary of data with this function 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.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • well I understand the concept, but do you have practical examples from which I can get started? – MettDich Jun 21 '17 at 10:44
  • Have a look at Apple's example code (i.e. [PotLoc](https://developer.apple.com/library/content/samplecode/PotLoc/Introduction/Intro.html#//apple_ref/doc/uid/TP40016176-Intro-DontLinkElementID_2) ) if you don't know where to get started, but the `WCSession` documentation explains how to set up the framework and then all you need to do is implement the sending and receiving functions on the two apps. – Dávid Pásztor Jun 21 '17 at 10:50
  • I try to look the potloc example but I didn't understand nothing.. instead..for updateApplicationContext(_:), where I've to call it? – MettDich Jun 21 '17 at 11:00
  • You should call it right after you update the label on your iOS app and send the updated String representing the current label.text to your Watch app. – Dávid Pásztor Jun 21 '17 at 11:02
  • I try something, but it seems like I don't have to add just 2 or 3 row, it seem like I've to add something like session, or other thing..I'm not able to find any example of complete code on my search.. – MettDich Jun 21 '17 at 11:11
  • As I've already told you, look at the official documentation of 'WCSession', it tells you exactly how to set up the session. If you really can't get through the documentation, have a look at [this tutorial from raywenderlich](https://www.raywenderlich.com/117329/watchos-2-tutorial-part-4-watch-connectivity), it gives a full working example of using the `WatchConnectivity` framework. – Dávid Pásztor Jun 21 '17 at 11:15
  • well..nothing, ray tutorial talk about too much thing for my actual level..I thought it was easy to pass a simple label.text on a watchOS label.text but I've had to add so many thing.. – MettDich Jun 21 '17 at 11:43
  • Once you get the hang of it, using `WatchConnectivity` is quite easy, but I agree, in the beginning it seems to much work for such an easy task. If you don't get the tutorial, you should first dig deeper into how Swift and WatchKit works and then continue with specific frameworks. – Dávid Pásztor Jun 21 '17 at 12:08
0

The right method for this is WCSession.updateApplicationContext(_ applicationContext: [String : Any]) https://developer.apple.com/documentation/watchconnectivity/wcsession/1615621-updateapplicationcontext "Sends a dictionary of values that a paired and active device can use to synchronize its state."

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57