Core Spotlight Framework - Adding NSUserActivity

We use Spotlight search very often to do a quick search and expect a result with a quick swipe search, it includes search for favorite contacts, best restaurant , latest news , recent banking transaction, etc.

Spotlight helps to enhance the user experience and improve discoverability of the content , finally land into your app.

It supports from iOS 9 onwards, but not available for below devices:

  • iPhone 4s
  • iPad 2
  • iPad (3rd generation)
  • iPad mini
  • iPod touch (5th generation).


CoreSpotlight framework provides API to have on device index , after which you can make it searchable publicly or private to your device , categories as "A private on-device index." and "Apple’s server-side index. "

With this discussion will implement a private on-device index.  Will use NSUserActivity and Core Spotlight API's.


A user performs many activities while using the app., for example in a Contact Activity the user is creating a favorite contact list. We would need NSUserActivity to add the item to on-device index.

Is the NSUserActivity default private?
Yes , the default item represented by NSUserActivity is private i.e it's a on-device index.

Is there ranking of preference to the indexed items?
Yes, items which are not found useful will stop showing. Ranking and relevancy is dependent on how often the item is searched, how much time the user spends when taps on the spotlight searched item and opens the activity.


Now lets NSUserActivity so that the item can be added on-device index. Please perform the below steps.

1) Create a NSUserActivity

 func addNewContact(){  
     let contactUserActivity = NSUserActivity(activityType: "com.test.activity.contact")  
     contactUserActivity.title = "Favorite Contact"  
     contactUserActivity.userInfo = ["id":"9"]  
     contactUserActivity.isEligibleForSearch = true  
     contactUserActivity.becomeCurrent()  
   }  

Below are the required properties of NUserActivity

  • activityType
  • title => The one that appears during the search"
  • userInfo => Dictionary which stores the data, when user tap's on activity 
  • isEligibileForSearch => Enable for indexing


2) Now add it any even or viewDidLoad:
   override func viewDidLoad() {  
     super.viewDidLoad()  
     addNewContact()  
   }  

3) Final Step
For the activity that user is going to perform in this case Show Favorite Contacts, we need to donate a shortcut for it.

This again very simple mention the activityType you define in step 1, in info.plist as one of the item in Array for name NSUserActivityTypes
      <key>NSUserActivityTypes</key>  
      <array>  
           <string>com.test.activity.contact</string>  
      </array>  

Now we run the app.

Will it execute and show the spotlight result?
No, we will have to make the contactUserActivity global such that it's reference is strong.

The final Code will look like this:
   var contactActivity:NSUserActivity?  
   override func viewDidLoad() {  
     super.viewDidLoad()  
     addNewContact()  
   }  
   func addNewContact(){  
     let contactUserActivity = NSUserActivity(activityType: "com.test.activity.contact")  
     contactUserActivity.title = "Favorite Contact"  
     contactUserActivity.userInfo = ["id":"9"]  
     contactUserActivity.isEligibleForSearch = true  
     contactUserActivity.becomeCurrent()  
     self.contactActivity = contactUserActivity  
   }  




This was the basic of the NSUserActivity helping to add the item for on-device indexing. Still more to come on Handling the index item tap event and how does your app. respond to it.

Comments

Popular posts from this blog

hitTest on iOS to identify the view being hit

CMTimeMakeWithSeconds explained

Custom Editing Control for UITableViewCell , like iPhone Mail Appliation