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

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

Jun 14, 2018 · 4 min read

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

Make a little interface for Apple Watch using WKInterfaceTable and managing IBActions

First of all, we need a little bit of theory of the possibilities and possibles communication scenarios:

— About Communicating with the Counterpart App

You initiate transfers and communication in any of the following ways:

👨🏽‍🎓Official Apple Documentation

- Use the method to communicate recent state information to the counterpart. When the counterpart wakes, it can use this information to update its own state. For example, an iOS app that supports Background App Refresh can use part of its background execution time to update the corresponding Watch app. This method overwrites the previous data dictionary, so use this method when your app needs only the most recent data values.

- Use the or method to transfer data to a reachable counterpart. These methods are intended for immediate communication between your iOS app and WatchKit extension. The property must currently be for these methods to succeed.

- Use the method to transfer a dictionary of data in the background. The dictionaries you send are queued for delivery to the counterpart and transfers continue when the current app is suspended or terminated.

- Use the method to transfer files in the background. Use this method in cases where you want to send more than a dictionary of values. For example, use this method to send images or file-based documents.

- In iOS, use the method to send data related to your Watch app’s complication. Use of this method counts against your complication’s time budget.

— Well, now you know that different possibilities what apple offers for communicating between both platforms, we can start to develop a series of tutorials on how to use the some of most important of them —

Create new project

We going to make a new project selecting the template for ‘iOS App with WatchKit App’ and continue basic steps to create it.

New Watch App

Apple Watch Interface

Now, we going to make a little interface for the Apple Watch composed by WKInterfaceTable and WKInterfaceButton, one just below the other. Next, inside of Table, we going to insert WKInterfaceLabel and set it the number of lines to 0.

Interface Builder aspect

Table Class and identifier

Going to TableRowController item on interface builder and put it the Class name, in my case Row and as an identifier set the same name. Should have the same aspect of next image

Interface builder connections

Now, connect the Table from Interface Builder to code in InterfaceController, and also connect the IBAction of the button.

New Class for manage cells on Table

Then, we going to make new a class named Row, and connect the WKInterfaceLabel located inside of TableRowController.

Time for coding

Now we going to implement a little bit of logic for manage all items on our list. A good way for that is using didSet to take action when the value is modified without needing another field.
In fact, we use it on our item property of type [String], and inside of didSet method, we call updateTable() method which set all items on our Table, following next code.

Note that, what the notifications occurs on background so we need to call main thread.

Run application

Then, we can going to testing application, so select Apple Watch simulator

Selecting simulator

You should be able to watch your Apple Watch simulator like that:


In the next series, we will continue with better content, but it’s necessary having a simple interface to play with communication between iPhone and Apple Watch.

See you soon! Cheers 🍻!!


Written by


iOS developer at ING Model Bank | iOS Lover | Valencian living on Madrid | Sometimes I try to be a writer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade