iOS 9: Quickly make your app support spotlight search

iOS 9: Quickly make your app support spotlight search

[[151508]]

iOS9 supports indexing the content in apps to support spotlight search, which is a very innovative feature. It should be mentioned that these indexes are stored in the local device and will not be synchronized to iCloud. If you change the device, they will be gone.

The effect is this:

Create search-enabled content

The class that supports searchable content is CSSearchableItem.

The attributes that can be displayed are title, description text, and thumbnail. It is recommended to set an expiration date (expirationDate) for each item.

First, create an object CSSearchableItemAttributeSet that represents a configuration display content

  1. let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeData as String)
  2. // Add metadata that supplies details about the item.  
  3. attributeSet.title = "July Report.Numbers"  
  4. attributeSet.contentDescription = "iWork Numbers Document"  
  5. attributeSet.thumbnailData = DocumentImage.jpg

AttributeSet also supports phone numbers and geographic coordinates. There will be corresponding actions on the right, and there will be an arrow if it supports navigation. (Because my app does not need this function, I have not tried it myself)

  1. attributeSet.phoneNumbers;
  2. attributeSet.latitude;
  3. attributeSet.longitude;

Creating a CSSearchableItem

uniqueIdentifier is equivalent to the id of this data. domainIdentifier indicates the related domain. Apple also provides a set of APIs to modify and delete these indexes. domainIdentifier can be used as a parameter, for example, all indexes under a domain can be deleted.

  1. let item = CSSearchableItem(uniqueIdentifier: "1" , domainIdentifier: "file-1" , attributeSet: attributeSet)

Add CSSearchableItem to the system

  1. CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([item]) { error in  
  2. if error != nil {
  3. print(error?.localizedDescription)
  4. }
  5. else {
  6. print( "Item indexed." )
  7. }
  8. }

Tips

Most apps may also need to be compatible with iOS8. Here is how to determine whether it is compatible with iOS 8 in Swift.

If you want to use iOS9 API in a method, use the following to judge (Xcode7 will also remind you).

  1. if #available(iOS 9.0 , *) {
  2. }

If you want to indicate that a method you wrote is only available on iOS 9, add the following keywords to the method header to indicate that it is only available on iOS 9.

  1. @available (iOS 9.0 , *)

I will paste my project code directly.

Processing of users selecting and opening the app after searching

Just add this callback in the app delegate.

  1. func application(UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: [AnyObject]? -> Void) -> Bool {
  2. }

The method under OC is this

  1. -(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:( void (^)(NSArray * _Nullable))restorationHandler{
  2. }

This can be used to get the identifier set when creating the CSSearchableItem

  1. NSString* identifier=userActivity.userInfo[CSSearchableItemActivityIdentifier];

Then you can use this identifier to retrieve the corresponding data and process it.

Reference Links:

  • Index App Content

  • Session 709 — Introducing Search APIs

<<:  Nanjing Hearthstone CEO Zhu Wei: Mobile game development can also be "unconventional"

>>:  Git for Dummies

Recommend

Private domain traffic: How to build a WeChat private domain closed loop?

“In 2019, the national population reached 1.4 bil...

Taobao Alliance Competitive Product Analysis Report

Online shopping has become a lifestyle for contem...

After Youtu marries Ali, can another big tree grow under the big tree?

There is always something big happening on Friday...

A breakdown of brand private domain operation strategies!

In recent years, new brands in the beauty and ski...

How to create a hit title? Master the 4 points!

Many people write very good content on WeChat art...

He has been gone for many years, but we still miss him...

If you were a drop of water Have you moistened an...