WatchOS 5 — Communication between iPhone and Apple Watch and vice versa on Swift — Part 5

This tutorial is written for WatchOS 5, Swift 4.2 and Xcode 10.0 beta (10L176w)

Lito
4 min readJun 26, 2018

What are you going to learn?

  1. Make a little interface for Apple Watch using WKInterfaceTable and managing IBActionsPart 1
  2. Request information from the iPhone application from the Apple Watch with sendMessage(_:replyHandler:errorHandler:)Part 2
  3. Send information from iPhone to Apple Watch sendMessage(_:replyHandler) Part 3
  4. Refactor our code for managing WCSession in one place. — Part 4
  5. Update application with updateApplicationContext(_:)Part 5

Update application with updateApplicationContext(_:)

The goal for today it’s knowing communication to counterpart with updateApplicationContext(_:), and first of all we remember what is the main its utility.

👨🏽‍🎓Official Apple Documentation

Use this method to transfer a dictionary of data items to the counterpart app. The system sends context data when the opportunity arises, with the goal of having the data ready to use by the time the counterpart wakes up. The counterpart’s session delivers the data to the session(_:didReceiveApplicationContext:) method of its delegate. A counterpart can also retrieve the data from the receivedApplicationContext property of its session.

This method replaces the previous dictionary that was set, so you should use this method to communicate state changes or to deliver data that is updated frequently anyway. For example, this method is well suited for updating your app’s glance.

You may call this method when the counterpart is not currently reachable.

This method can only be called while the session is active — that is, the activationStateproperty is set to WCSessionActivationState.activated. Calling this method for an inactive or deactivated session is a programmer error.

Well, knowing that, you should able to continue with implementation, and imagine the situation for use it.

Implementation for both ⌚️📲

First of all, we going to make a new swift Class named Constants.swift to obtaining a shared dictionary, putting the ‘Target Membership’ for both and adding it within the Commons folder.

Then, you add the next code:

The before code, it’s for select our theme, composed with an array of tuples, to implement the data for UIPickerView and WKInterfacePicker.

Now, we go to our WatchSessionManager.swift and we need to add next code (only those that have an enumerated comment):

WatchSessionManager.swift

Well, if you Build the application, the compiler tells you that must you to implement the stubs, if like that, all run perfectly. 🤘🏽

Now we going to implement the required logic for each platform.

Well, now we need to make a little interface to be able to change the theme in both applications and updating it if you change it from the Apple Watch or from the iPhone. The interface consists of one UIPickerView/WKInterfacePicker for selecting a colour, one for iPhone and another for Apple Watch.

⌚️ Apple Watch implementation

Then, we going to Interface.storyboard and add a WKInterfacePicker below of our buttons, and connect IBOutlet with the code.

Should look like how the before image

Next, we also need to add IBAction of the MKInterfacePicker called for example changeTheme(_ value: Int) and connect existent Buttons from the code to be able to change colours.

We only use updateApplicationContext() for transfer the last index selected to counterpart

Now in our controller InterfaceController.swift we should have a next lines of code:

InterfaceController.swift

Well, I going to explain the main points:

// 1: These are the Outlets for manage views.
// 2: On awake method, we need to establish our common data for paint all items of a picker.
// 3–4–5: We have an event for manage logic when the user moving the picker we launch changeTheme and call our WCSessionManager for update applicationContext with the index selected.
// 6: Method for update theme (change the background of buttons).
// 7: In this point, we only manage received context calling changeTheme()

On the part of Apple Watch, this would be the necessary logic.

📱 iPhone implementation

The first task, is going to storyboard and set UIPickerView and connect it to code, also you need to connect the UIButton for can change colour.

Main.storyboard

Next, we need implements the dataSource on the Picker and delegate when the user change item on it.

Open ViewController.swift and add next code:

How you should be noticed, the main function is 4th on didSelectRow method, when we send an update to Apple Watch, using our WCSessionManager. And in 3rd we manage received context.

At the moment the implementation it’s very easy thanks to the Manager named WatchSessionManager, we only focus on interface implementation.

If you were able to get here, you can launch the application and watch the next functionality on your simulator:

Results for didReceiveApplicationContext()

Conclusion

Well, now you can use didReceiveApplicationContext in your projects to update status application to the counterpart.

See you in the next part of this series of tutorials.

You can download code here

See you soon! Cheers 🍻!!

Collaborate

If you want and you liked the content you can support this writer, thank you very much 😀

--

--

Lito

iOS developer at Kairos DS | iOS Lover | Valencian living on Madrid | Sometimes I try to be a writer