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)
What are you going to learn?
- Make a little interface for Apple Watch using WKInterfaceTable and managing IBActions — Part 1
- Request information from the iPhone application from the Apple Watch with sendMessage(_:replyHandler:errorHandler:) — Part 2
- Send information from iPhone to Apple Watch sendMessage(_:replyHandler) — Part 3
- Refactor our code for managing WCSession in one place. — Part 4
- 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
updateApplicationContext(_:)
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
sendMessage(_:replyHandler:errorHandler:)
orsendMessageData(_:replyHandler:errorHandler:)
method to transfer data to a reachable counterpart. These methods are intended for immediate communication between your iOS app and WatchKit extension. TheisReachable
property must currently betrue
for these methods to succeed.- Use the
transferUserInfo(_:)
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
transferFile(_:metadata:)
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
transferCurrentComplicationUserInfo(_:)
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.
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.
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
You should be able to watch your Apple Watch simulator like that:
Conclusion
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 🍻!!
Collaborate
If you want and you liked the content you can support this writer, thank you very much 😀