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

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

Lito
4 min readJun 21, 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

Refactor our code for managing WCSession in one place

Well, first of all, we going to open the project and remove the file SessionHandler.swift and start to make new wrapper/manager for manage all logic of WCSession from the same one place, both for Apple Watch and for iPhone.
The reason is that the delegates are the same in both cases, only changing implementation, obviously for each platform has different logic to develop. Therefore we going to make an abstract and reusable class to manage that communication between both platforms in all of our projects what use Apple Watch.

Implementation for both ⌚️📲

Ok, add new group/folder on our root target project named it ‘Commons’ and also within that new group add new Swift file named WatchSessionManager.swift, next we need share it on two platforms checking the check ‘Target Membership’ for both targets as in the following image:

Target Membership
Tree project

Then, we going to implement the new manager of WCSession named WatchSessionManager.swift, and this is how he looks:

Ok, the code is commented but I explain the more important things:

First of all, I decided to use two delegates one for each OS platform, because it’s independent modules, and only found two possibilities or with delegates or through closures and my decision is obvious, force user to use implementation methods on its delegates classes. If you know other possibilities I’m opened to listen to you.

In point 1, 2 and 3 you can see the current two delegates with one method per each, and for not duplicate logic I made a typealias mechanism, this is not dispensable, but our code it’s more beautiful.

In point 6, I use a single line condition for knowing if session isSupported() on the contrary returns null.

The 7 point is very important, knowing if session is available or not , This is possible for iPhone device and you must check if session isPaired() and isWatchAppInstalled() but in Watch this is not possible, thus we use a conditionally compile (#if os(iOS)) for know which is the platform and doing it or not, the same occurs on delegate methods on point 9.

From the point 11, we have all the logic of sending messages or receive it.
In 13 point we have the two sender methods, with replyHandler or without replyHandler, for both iPhone and Apple Watch.
In 14th we have the receiver methods, also for both, and we wrap these two methods in handleSession (point 15) method which is the manager for emit on the correct delegate of a platform and set the reply or not.

Then, this is all for manage WCSession in one place, now we going to implement this class on our iPhone and Watch target.

📱 iPhone implementation

First of all, we going to our AppDelegate.swift and change for next code:

The ViewController.swift will have this aspect:

Implementation for both ⌚️📲

To manage a different kind of requests to iPhone we must create enum for both targets, one kind of request is getting Date and another one getting iPhone Version:

RequestType.swift

You can add more request types as you need.

⌚️ Apple Watch implementation

Well, we going to Interface Builder of Apple Watch, and change your interface adding two buttons, one for each action:

And connect IBAction’s to code, we will have two methods requestDate() and requestVersion().

Then, next is our code on InterfaceController.swift:

InterfaceController.swift

If you launch the app, you can watch the this behaviour next image:

Conclusion

Perfect, you can run the application and test it, you should to send messages from iPhone to Apple Watch, and request information from the iPhone to Apple Watch.. we have the same behavior that before but we have code more readable and elegant.

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