ios-9-searchHistorically, iOS has limited its search capabilities to the web and/or system apps that come pre-installed with iOS. With the release of iOS 9, though, Apple is enabling developers to take advantage of search to make their app’s content more accessible than ever.

Apple has provided three options for implementing search on iOS 9. I’ll be covering these in detail over the coming weeks, but today will focus on my favorite: Activity Indexing.

Why Implement Search?

Accessing search in iOS 9 provides a new way for users to interact with their devices. Let’s take the case of a user who wants to learn about a particular wine. The user will no longer need to search the App Store to find a wine app; they will simply ask Siri or use iOS search. iOS will search the publicly indexed data from other users’ wine review apps and return the most popular results. The user can then click on the result and be taken straight to the App Store page, ready to install the app. Once installed, the app launches and takes the user straight to the wine they were initially interested in learning about. If you’re not indexing your data, your application’s content will not be discoverable and you will struggle to compete for downloads.

Get started

First you’ll need to ensure you’ve got the latest version of the Xamarin.iOS 9 Preview and the latest Xcode beta installed. Make sure you open Xcode at least once after installing it to ensure it is initialized and has finished installing its necessary components. You’ll then need to point Xamarin Studio to the Xcode beta.

Screen Shot 2015-09-01 at 14.51.40

Monkey Searching

In this example, I’ve got a list of monkeys that I wish to make searchable outside of my app. The data is coming from a helper class, which contains a list of monkey objects. You may be familiar with this demo, which my colleague James Montemagno originally Simulator Screen Shot 1 Sep 2015 14.24.52created with Xamarin.Forms. Using this dummy data, I populate a UITableView with the names of the monkeys. At this point, the monkey data isn’t accessible to iOS 9. We know this because when I search for a squirrel monkey using Spotlight, I only receive results from the iBook store and Wikipedia.

In order for iOS to know that my app contains information about squirrel monkeys, I’m going to create an NSUserActivity, make it eligible for search, and set a couple of properties. If you’re unfamiliar with NSUserActivity, Apple added it in both iOS and OS X last year to allow developers to easily capture application state. This enables handoff to pass the application state between a Mac and iPhone or other iOS device.

NSUserActivity CreateActivity()
{
    var activity = new NSUserActivity(activityName);
    activity.EligibleForSearch = true;
    activity.EligibleForPublicIndexing = true;
    activity.EligibleForHandoff = false;

    activity.Title = monkey.Name;
    activity.AddUserInfoEntries(NSDictionary.FromObjectAndKey(new NSString(monkey.Name), new NSString("Name")));

    var keywords = new string[] { monkey.Name, "Monkey" };
    activity.Keywords = new NSSet(keywords);
    activity.ContentAttributeSet = new CSSearchableItemAttributeSet(monkey.Details);

    return activity;
}

I call the above code every time the user enters the details page of a monkey. If the user taps on ‘Baboon’, we’ll add it to the index, ready for iOS search to discover. Now when searching for squirrel monkeys, my app is listed as a top hit.

Simulator Screen Shot 1 Sep 2015 14.25.37

 

Wrapping Up

Search is going to play an essential role in iOS 9 and will change the way users discover useful apps. I’d say that adding search to my apps is my number one priority for the iOS 9 update, and you can see thats it’s extremely straightforward to get started. You can learn more about NSUserActivity and download a sample project on my GitHub