Adopting Handoff in the Extension Delegate

Handoff is a technology permeating many Apple platforms: iOS, OS X, and watchOS. It relies on the concept of a user activity, which describes the thing that the user is doing. This could be editing a document, ordering a pizza, or playing a game. On iOS and OS X, adopting Handoff and user activities allows you to transfer that activity from one device to another. You can seamlessly edit a document on one device and then open the same app on another device right to the same document. On watchOS, Handoff has two main purposes: to continue an action on the user’s iPhone, just as you can among other Apple devices, and to navigate into the watch app from other user interfaces, such as the app’s Glance. A simple calculator app on watchOS, for instance, could use Handoff to open its counterpart on iOS where more advanced calculator features are possible. For a Glance, using Handoff inside the app helps you to open the app right to the relevant portion covered by the Glance. For example, an airline watch app’s glance might show you your next flight details, while the watch app might open to your profile screen with frequent flier points and such; using Handoff from the Glance, you’d open the watch app right to the next flight details.

Unlike iOS, where you’d create an NSUserActivity instance yourself and mark it as the current activity, watchOS relies on your interface controller to provide activity data indirectly using the updateUserActivity(_:userInfo:webpageURL:) method. The three arguments you provide give your other devices information about how to handle the user activity: an identifier for the activity, such as “com.pragprog.readingabook,” an optional userInfo dictionary to pass more information about it, and an optional webpageURL parameter to open a website on a device without an app that can handle the activity. Using this method in WatchKit interface controllers automatically sets up Handoff for properly configured apps, but that’s not all it can do.

In an app’s Glance, you can’t add interactivity of any kind. No buttons, pickers, or anything of that sort. So what happens when the user taps a Glance? The answer lies in Handoff. watchOS piggybacks on the Handoff system to open apps directly to specific points. For instance, if you have a Glance relating to a specific part of the app, the extension delegate can handle the user’s tap and navigate to the correct area of the app. First, implement updateUserActivity(_:userInfo:webpageURL:) inside the glance, including some information about the user’s activity in the userInfo parameter. Then in the extension delegate, implement handleUserActivity(_:). In this method, parse the userInfo to determine the appropriate course of action. Just as you returned to the run timer in TapALap if the app was launched while a run was in progress, this method is the perfect time to switch the app’s UI to the appropriate screen.

Although watchOS doesn’t have any hooks for using Handoff to launch apps from other devices, there’s one more way to use Handoff: to launch an app from the watch face. You’ll learn more about that when you learn about complications later in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.200.161