From the course: Apple watchOS App Development: Advanced APIs

Implement watch connectivity

From the course: Apple watchOS App Development: Advanced APIs

Start my 1-month free trial

Implement watch connectivity

- [Instructor] When you want to communicate between an iPhone and an Apple Watch, you have some preparation work to make the connectivity framework do its magic on both sides. You'll activate the watch connectivity framework, and set up a few delegate methods before communicating. Head to the InterfaceController in the Watch Extension. First you need the new framework, so add that with import WatchConnectivity. Secondly, you'll need the WCSessionDelegate. Add that to the InterfaceController. And there it is, the one WCSessionDelegate. Now, you're going to get an error message about not conforming to the protocol. Hit the Fix Button. Save you some typing, and we'll get the one required method session activiationDidCompleteWithactivationState. Now, I like my delegates all the way at the bottom of my code, so I'm going to Cut that Out, Slide all the way to the Bottom of My Code, down here. Give myself a bunch of space, and Paste It here. On the watch side, this is the one required method, which launches after activation, and let's you check the activation state. There are three states, activated, not activated, and inactive. I'll put a switch in here to check the state of activation state, and print an appropriate message for now. So we'll do here is switch, activationState, and then in the Braces, I'm going to start with case dot, and we'll start with activated:, and we'll print, ("Watch WCSession Activated"), and they're pretty much going to be the same thing, and I'm just going to print each one of these. So what I'm going to do is Copy that. I'm going to make two more copies, and I'm going to change the second one here to notActivated. And just Add Not Activated. And this one is inactive. I'll just Change Activated to Inactive. Now, our next step is to head up to Awake, and so here we are in awake(withContext). And I'm going to Add to that what I need to activate all this up and that's, first of all, I'm going to need a session. So I'm going to call it let session = WCSession. And there's a singleton called default, which is our shared session for everybody. And I'm going to use a session.delegate. Go to my delegate works, and that's going to be self. And then I'm going to activate the session. SessionActivate, is very simply activate. Now we're ready from the watch side, so let's go to the phone side. And that'll be up here in ViewController. Slide all the way to the top. Once again, we've got to import the framework. Import WatchConnectivity. Again, we're going to need the WCSessionDelegate, and again, I'll get lazy here, and use the error message to get me my required methods. There are three this time, and I'm going to copy all three, or cut all three, and do what I did last time, which is for the watch, just go all the way here. Stick them on the bottom, so they're easy to find. Now, this one here works the same as the one in InterfaceController. So what I'm going to do is, I'm going to go down in the InterfaceController down to session, the one we've already made, and I'm going to copy this whole thing. Go back to the ViewController, and I'm just going to replace this with a paste, and then all I have to do is change this to Phone, save myself some typing. And then under that, we have two more methods, which indicate when the current watch goes inactive or deactivates. This is for handling users who have more than one watch, and switch watches. I'll add print statements in them, so that we can see them fire in the console if they do. So I'm going to do this as just a print statement, and I'm just going to say ("Session went inactive"), and again, I'm just going to copy that with a Command + C, and I'm going to Paste it here, and I'm just going to put deactivated, like so. Alright we're going to do another one of those copy type things, because the code is so similar. I'm going to go back up to awake(withContext) again, and my activation code here that we had before from the watch, I'm going to copy that as well. Then go back up to the ViewController, go to ViewDidLoad, and before I paste it, I'm going to add an extra something okay? But first I'm going to make it to say //Watch Connectivity, so I know what it is. And now I'm going to put an if statement here. Because there's one more piece that we have to be careful about. Not all iOS devices can use watch connectivity. An iPad for example, can't. So what I'm going to do is call WCSession, and a property of that, which is called isSupported. If the device is supported, this'll be true, otherwise it'll be false. And so, it'll only take care of the supported devices, and I'll only connect to those, and now I'll put that code that I had in the watch. The watch always connects, so I don't have to worry about this in there. Alright, so we're set to go. And at the top, we'll make sure that we've got our watch scheme. Okay, and go ahead and run that. And you'll see at the bottom, here in the console, that it says that our watch WCSession activated. So this is the basic setup for watch connectivity. Once set up, we can begin to send messages between devices.

Contents