Handoff from  Watch to iOS app

download source

On the Watch side, in a button click responder, we call updateUserActivity method, which is a way to hand off data that the user is currently working on. This method is part of WKInterfaceController.h.

InterfaceController.m

Notice the string key com.rtsao.handoff.

You need to go to your project folder’s Info.plist, add NSUserActivityTypes as an Array, then in that array, add com.rtsao.handoff

handoff-info-1

Then in your Watch Extension folder’s Info.plist, do the same thing.

hand-off-info2

iOS app

AppDelegate.m

Run the program. Also, click the power button on your iphone to lock it.

When your  watch turns on, click on the button to activate the handoff. It calls the updateUserActivity method, which means its ready to pass off the current User’s activities to the paired iOS app.

Now click on the home button of your iphone. You’ll see that your iphone is on the “unlock screen”. You will also see that your app icon appears on the lower left hand side. This means that once the  watch passes off the user’s activity to the iOS app, you can activate the continuation of that user’s activity by sliding this icon up.

handoff-appicon

Once you slide up, your iOS app will appear, and the thread of execution will continue in AppDelegate.m’s continueUserActivity. The continueUserActivity method is part of UIApplication delegate protocol methods.

Look at the parameter userActivity. That contains all of the data you need to start processing the user’s activities. Use the activityType to retrieve the id, and the userInfo to get the data, among other data.

Also notice the restorationHandler block. This block takes an array of UIViewControllers. It then sends each of these UIViewControllers a restoreUserActivityState: message, passing in the resuming activity’s NSUserActivity object.

The window controllers inherit the restoreUserActivityState: method from NSResponder, and each controller object overrides that method to configure its window, using the information in the activity object’s userInfo dictionary.

Hence since we only have 1 main ViewController, let’s pass that in. In is simply self.window.rootViewController.

Then in our ViewController.m, we implement:

You now have the user’s activity in your particular UIViewController and its view hierarchy. Thus, this let’s you take care of processing the data in whatever part of your app.