WatchConnectivity Framework Example

3 minutes, 16 seconds

WatchConnectivity is an important framework when building WatchKit app. With WatchConnectivity framework, data and files integration between iPhone app and paired WatchKit. It’s intoduced since watchOS 2.0 and integrate with iOS 9.0.

Now how do we use it? Let’s go through some simple example. We send data from iPhone to Watch app.

Let’s start from the iPhone app. I assume you will create an single view app with ViewController.swift file. And also already have your WatchKit App and WatchKit Extension.

Take me to WatchKit App

iPhone App

Add this on the header.


import WatchConnectivity

Add a variable of WCSession.


var session : WCSession!

Inside configure viewDidLoad and add a method to check the watch connection status, so we can see on the log. Here we just check if WatchConnection is supported, and activate the WatchKit’s session.


override func viewDidLoad() { super.viewDidLoad() if WCSession.isSupported() { session = WCSession.default session.delegate = self session.activate() watchConnectionStatus() } } func watchConnectionStatus(){ print("isPaired",session.isPaired) print("session.isWatchAppInstalled",session.isWatchAppInstalled) print(session.watchDirectoryURL) }

Of course add the delegate on the class definition.


WCSessionDelegate

Implement delegate’s methods.


// MARK: - WCSessionDelegate func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { watchConnectionStatus() } func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { } func sessionDidBecomeInactive(_ session: WCSession) { } func sessionDidDeactivate(_ session: WCSession) { }

It’s up to you to use your own UI, but here we will send a String with WCSession. We send as a Dictionary, with key and the value. You may change the string of the value with something you get from UITextField.


@IBAction func sendMessage(_ sender: Any) { session.sendMessage(["message":"My Messege"], replyHandler: nil, errorHandler: nil) }

WatchKit App

At your WatchKit app, there is a InterfaceController.swift, add this framework at the header.


import WatchConnectivity

Also declare the WCSession variable.


var session : WCSession!

Configure the willActivate method.


override func willActivate() { super.willActivate() if WCSession.isSupported() { session = WCSession.default session.delegate = self session.activate() } }

Don’t forget to declare conforming to WCSessionDelegate.


WCSessionDelegate

Then implement neccessary methods.


// MARK: - WCSessionDelegate func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { } func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) { let message = message["message"] as! String print(message) replyHandler(["msg":"successfully sent from iPhone"]) }

Here at didReReceiveMessage method we will receive a message from iPhone. With this method we also can send back message back to the iPhone.