How to Use Proactive Suggestions for iOS 10

Continued from page 1.

As the name implies, the Handoff API library allows apps to change the control handoff from one platform (such as iOS) to another (such as OS X). iOS 10 enhanced NSUserActivity with features specific to intelligent suggestions.

NSUserActivity, as an interface class, permits the capturing of user states throughout the lifecycle of an app, capturing the key moments during the interaction sequences the user makes. For instance, when an user searches on Yelp for a specific restaurant, NSUserActivity would then capture the restaurant’s address.

  • … You create user activity objects and use them to capture information about what the user was doing, such as viewing app content, editing a document, viewing a webpage, or watching a video. When the system launches your app and an activity object is available, your app can use the information in that object to restore itself to an appropriate state. (source: Apple)

The developer indicates the key states to capture in the app as candidates for Handoff. The location is temporarily made available throughout the system for location suggestions, as well as added to the spotlight device indexing. The developer declares the activity types supported by including the NSUserActivityTypes key in the info.plist file, which the system then uses to determine whether the app is capable of handling a given user activity object.

To instantiate an NSUserActivity object for the aforementioned restaurant example, the code would look something like:

let restaurantActivity = NSUserActivity(activityType: "com.programmableWeb.view-location")
restaurantActivity.title = "Joe's Burgers"

//set capabilities of activity
restaurantActivity.isEligibleForHandoff = true
restaurantActivity.isEligibleForSearch = true
restaurantActivity.isEligibleForPublicIndexing = true

In the above code, the developer notifies the system of which tasks the app is capable of handling. The developer finally sets the userInfo payload of the activity with the attributes to capture and to restore later on, using the same id:

restaurantActivity.userInfo = ["id" : "mission-taco-diner-mission"]

The developer then describes the activity through the use of the CSSearchableItemAttributeSet class reference, providing enough metadata to facilitate greater contextual information around that activity:

let attribs = CSSearchableItemAttributeSet(itemContentType: "com.programmableWeb.view-location"
attribs.thumbnailURL = imgURL
restaurantActivity.keywords = ["burgers", "restaurant", "mexican restaurant", "taceria food"]
restaurantActivity.contentAttributeSet = attribs
restaurantActivity.webPageURL = ""

The last attribute, webPageURL, provides an avenue for when the user hasn’t installed the developer’s app. It makes it easy to include an automatic redirect option to the specific Web URL via Safari, instead.

Now, in the UIAppDelegate, the developer can handle incoming activity calls as follows:

func application(UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: [AnyObject]? -> Void) -> Bool {
 //determine which activity we are handling, denoted by the ID
 if userActivity.activityType == "com.example.view-location" {
   //handle call to bring up screen for activity
   return true

With that simple implementation, apps can now handoff from one device to other devices, as well as index within spotlight search and support Contextual Siri Reminders. Contextual Siri reminders enable users to say to Siri, “Remind me of this,” whereupon the iOS Reminders app adds a deep link reference to the contextual location within the user’s active app.

Integrate with Spotlight Search

Spotlight Search, introduced last year, uses Search APIs to help developers promote their apps directly within Spotlight using NSUserActivity and Core Spotlight.

Incorporate Location Suggestions

iOS 10 adds more to NSUserActivity with location suggestions, which capture location information from inside an app and later use it elsewhere.

The obvious example continues to be restaurant information (because it’s lunch time after all, and we are all thinking about those enchiladas). Remember how Sally was looking at an an app that lists local restaurants? Because its developers expose the restaurant location information, when Sally switches to Apple Maps, Maps tries to figure out if she is looking for directions and provides quickType suggestions to that particular restaurant.

Be sure to read the next Search article: Bing Search API Tutorial