---
title: "#267: Using the mapItem Property on NSUserActivity 📍"
url: https://littlebitesofcocoa.com/267-using-the-mapitem-property-on-nsuseractivity
author: Jake Marsh
date: 2016-09-22
published: 2016-09-22
topics: ["Continuity"]
description: "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..."
---

# #267: Using the mapItem Property on NSUserActivity 📍

By Jake Marsh, September 22, 2016. HTML: https://littlebitesofcocoa.com/267-using-the-mapitem-property-on-nsuseractivity

We first looked at **Handoff** and `NSUserActivity` all the way back in [Bite #29](https://www.littlebitesofcocoa.com/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 <i>Robot Stores</i>. We'll start by re-using a technique we first learned about in [Bite #47](https://www.littlebitesofcocoa.com/47), **MKLocalSearch**.

First we'll create an `MKLocalSearchRequest` to find all the <i>Robot Store</i> locations within a reasonable radius of the user's **current location** (acquired off-camera via standard **Core Location** mechanisms):

``` swift
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 <i>Robot Store</i> 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`. 

``` swift
MKLocalSearch(request: request).start { (response, error) in
  guard error == nil else { return }
  guard let response = response else { return }
```
<p class='break'></p>
``` swift
  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:

<img src="https://media.littlebitesofcocoa.com/267-1.png" style="max-width: 628px;" />

Success!
