We first looked at Handoff and NSUserActivity all the way back in Bite #29. Today we'll take a look at a new addition to NSUserActivity in iOS 10 that allows our apps to share more about the user's current context with other parts of the system. Let's dive in. 🏊

Let's imagine our app lets users view a list of Robot Stores. We'll start by re-using a technique we first learned about in Bite #47, MKLocalSearch.

First we'll create an MKLocalSearchRequest to find all the Robot Store locations within a reasonable radius of the user's current location (acquired off-camera via standard Core Location mechanisms):

let request = MKLocalSearchRequest()

request.naturalLanguageQuery = "Robot Store"
request.region = MKCoordinateRegionMakeWithDistance(
  usersCurrentLocation, 1600, 1600)

Next, we'll start an MKLocalSearch using our request, and load a bunch of mapItems representing Robot Store locations near us.

If we were building a complete app, this is the part where we'd grab the response.mapItems from above, map their values into some business objects, then display them probably in UITableView, etc.

For now, let's simply try out the new feature of NSUserActivity.

MKLocalSearch(request: request).start { (response, error) in
  guard error == nil else { return }
  guard let response = response else { return }

  guard let exampleMapItem = response.mapItems.first else { return }

  let activity = NSUserActivity(activityType: "com.robots-store.shopping-locally")

  activity.title = exampleMapItem.name
  activity.mapItem = exampleMapItem

  self.userActivity = activity
}

Finally, we can run our app, double tap our home button and see the results:

Success!

Similar Bites