Using Spotlight Search In Your Apps

      No Comments on Using Spotlight Search In Your Apps

You can easily integrate with Spotlight. In this article we'll get you started with Spotlight so you can start using Spotlight Search in your apps.Spotlight is a great feature that can make life a lot easier for your users. It’s build into the system and the users are familiar with it. You can easily integrate with Spotlight by indexing your data and adding it to the spotlight index. In this article we’ll get you started with Spotlight so you can start using Spotlight Search in your apps.

The App

For this demo I’ll be using one of my apps. You should be able to easily adapt the code in this article for your own apps. The app that I’ll be using is a simple app that reads the feed from this blog. The data model will certainly be different from your app, but you can easily apply what you learn here to your app.

The Code

We’ll create an extension for the data model. This is the data model that I’m using in this app:

First, let’ import the CoreSpotlight and CoreServices:

Now we’ll create an extension of our data model that will make our lives a lot easier:

The ‘attributeSet’ private variable is where we’re setting the meta-data that we’ll need for Spotlight. As you can see, it’s pretty simple. We’re using the ‘kUTTypeCompositeContent’ as the content type because the content is a blog article. In your case it might be different. The keywords are important, as you might imagine ๐Ÿ™‚ In this example we’re just using the strings from the title that are longer than two characters. We could be setting the thumbnail image here as well, but the images in this app are downloaded asynchronously so this would just complicate the example. If you’re interested, you can play with it yourself. Here’s how you would set the thumbnail image:

‘searchableItem’ is the public variable that we’ll be using for Spotlight. The ‘CSSearchableItem’ object is really simple. It only takes a pair of identifiers and the attribute set that we created earlier. The ‘uniqueIdentifier’ should be unique for the object. We’re embedding the object ID in the unique identifier, because we’ll need it later to fetch the original object from our data source. The ‘domainIdentifier’ is not really necessary in this example but you can use it to group different object types. Then, if you wanted to, you could easily delete all the data for a certain domain identifier.

Next up is adding our post items to the index. We’ll have a function that will add our items to the index. It’s up to you when you want to call this function. Usually you would call it right after getting the items from the server. Thanks to our helpful extension our function is going to be pretty simple:

Since we’re using a domain identifier, we can easily delete all the posts from the index:

Of course, you can delete individual items as well by using the unique identifier.

Handling User Action

When the user performs a search using Spotlight and selects your result object, the app will be called with that object. Of course, you want your user to land on the content that they were searching for. So we have to implement one function in the app delegate:

As you can see, we’re extracting the post id from the unique identifier. With this, we can fetch the original object from the data source and show it to the user. Your app will probably be a bit different, you might be presenting a view controller modally, for example.

Final Look

All that’s left is to take our app for a spin. Start it up, let it finish downloading the data and indexing, press the home button and pull down to show Spotlight. If you search for an article that got indexed by Spotlight you should see something like this:

Now, select one of the search results and see it displayed in your app. Pretty cool, right ๐Ÿ™‚

Conclusion

Adding search functionality into your apps can be pretty trivial if you use CoreSpotlight. Seamless integration with iOS and a happy user are just added bonuses ๐Ÿ™‚

In this article we’ve covered some basics to get you started and you should have enough info to implement it into your own apps. I hope you had some fun learning new things today ๐Ÿ™‚

Have a nice day ๐Ÿ™‚
~D;

More resources

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.