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

California approves Google Waymo to transport passengers on driverless roads

In the wave of AIoT and 5G, autonomous driving ha...

Stay away from big trucks! The driver really can't see you!

Audit expert: Zhu Guangsi Member of Beijing Scien...

Chen Jiangxiong-Douyin Follower Increase and Traffic Training Camp

Chen Jiangxiong's Douyin fan increase and tra...

Make way for the super ferocious big cat!

Recently, a man found a black animal This animal ...

Is the quantum measurement problem a problem?

The problem of quantum measurement is an unavoida...

iOS speech recognition wave animation based on Speech framework

Author : Wu Xinshuang, Family Operations Center L...

In the dark forest, have we been exposed?

In 1920, General Motors hired American inventor C...

Nut 3 review: Outrageous balanced design and battery life will move you to tears

As a thousand-yuan phone, the Nut 3 has achieved ...