Show Menu

Expanding your company? Need a developer?

Submit your 30 day Job Listing for FREE

watchOS 2 Hello World tutorial Logo

In this watchOS 2 Tutorial I am going to show you how to check if there is a connection between your iOS Application and your watchOS. We will do this by checking if each of the devices can receive signals from eachother using the methods: sessionWatchStateDidChange(_:) & sessionReachabilityDidChange(_:) which are found in the WatchConnectivity framework.

There is a property called Reachable (Type: Bool) inside WCSession which does two things; on your iOS Application it tells you whether there is a reachable watchOS app and on your watchOS app it tells you whether the companion iOS application is reachable.

Firstly, in your AppDelegate.swift file, just under the import UIKit we need to include the WatchConnectivity


import WatchConnectivity

WCSessionDelegate

Then find the following lines of code:


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

Now we need to make your app delegate a delegate of the WCSession too by adding WCSessionDelegate. So change the lines above to the following:


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

    var window: UIWindow?

Get WCSession states

I’m going to show you how to create an extension of WCSession which will allow you to print out all of the states that are found in sessionWatchStateDidChange(_:). These states are;

  • paired,
  • watchAppInstalled,
  • complicationEnabled,
  • watchDirectoryURL

Above the lines of code we just added WCSessionDelegate and below the import WatchConnectivity to lets make this extension:


extension WCSession{
	public func printInfo(){

		//paired
		print("Paired?: ", terminator: "")
		print(self.paired ? "Yes" : "No")

		//watch app installed
		print("Is Watch app installed?: ", terminator: "")
		print(self.watchAppInstalled ? "Yes" : "No")

		//complication enabled
		print("Is Complication enabled?: ", terminator: "")
		print(self.complicationEnabled ? "Yes" : "No")

		//watch directory
		print("The Watch directory url", terminator: "")
		print(self.watchDirectoryURL)
	}
}

The next thing to do is to create a listener so that we can detect whether these reachability statuses change. Just under the line: var window: UIWindow? add in these two functions:


func sessionReachabilityDidChange(session: WCSession) {
	print("Is Reachable: ", terminator: "")
	print(session.reachable ? "Yes" : "No")
}

func sessionWatchStateDidChange(session: WCSession) {
	print("The Watch state has changed")
	session.printInfo()
}

Finally, we are now going to create a session that will check these for us. We will be adding these into a function so that we can make sure that it will be called even if the app is running in the background:


func checkSession() {
   guard WCSession.isSupported() else {
     print("Session is not supported")
     return
   }

   let session = WCSession.defaultSession()
   session.delegate = self
   session.activateSession()
}

As you can see in the above code I used the Swift guard statement to check for a false. If this doesn’t make sense, check out this tutorial: Introduction to guard syntax

And finally from the iOS Application side we need to add the above function checkSession() into the application function. Like so:


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    checkSession()
    return true
  }

Now we are going to start the coding for the watchOS side. YAY… In your ExtensionDelegate.swift file, just like we need in the App Delegate we need to make sure that it is a delegate of the session and make sure we include import WatchConnectivity. We do this by adding WCSessionDelegate and the import into file like so:


import WatchKit
import WatchConnectivity

class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {

We now need to add in another listener on the watchOS side like so:


func sessionReachabilityDidChange(session: WCSession) {
  print("Reachablity has changed to: ", terminator: "")
  print(session.reachable ? "Reachable" : "Not Reachable")
}

Finally, in the applicationDidFinishLaunching() we need to create our session:


guard WCSession.isSupported() else {
  print("Session is not supported")
  return
}

let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()

That’s all folks. Run the app and test it out. Pretty cool right?

Summary

In this watchOS tutorial I have shown you have to check the reachability of both devices from eachother. You now know how to obtain a session with WCSession.defaultSession(), Set the delegate property of your session. How to make the delegate a delegate (of type WCSessionDelegate) of the session. You should also now know how to Implement the sessionWatchStateDidChange(_:) function of your session delegate and in there, check the reachable flag of the session and finally how to Call the activateSession() method of your session.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

I work in the shadows. You can thank me by being a good soul.

Comments

comments