天天看點

iOS9系列專題二——全新的搜尋功能api(一)更加智能的搜尋方案——iOS9搜尋功能新api

更加智能的搜尋方案——iOS9搜尋功能新api

一、引言

       iOS9中為我們提供了許多新的api,搜尋功能的加強無疑是其中比較顯眼的一個。首先,我們先設想一下:如果在你的app中定義一種辨別符,在siri和搜尋中,可以用過這個辨別符搜尋到你的app,是不是很棒?不,這還差得遠,你可以定義任意的資料,使其在搜尋和siri中可以快速檢索到,這樣的搜尋功能是不是非常酷?不,還有更cool的,你甚至可以在你的網站中添加一些标志,使apple的爬蟲可以檢索到,那樣,即使使用者沒有安裝你的app,也可以在搜尋中擷取到相應的資訊,這太強大了,對吧。

二、3種全新的搜尋模式

‍1、NSUserActivity‍

       我們可以在項目中使用相應的函數來添加一些使用者的活躍元素,使我們可以在搜尋中通過搜尋這樣的活躍元素展現我們的app。例如:

   //建立一個對象,這裡的type用于區分搜尋的類型

   NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType: @"myapp"];

   //顯示的标題

   userActivity.title = @"我的app";

   // 搜尋的關鍵字

   userActivity.keywords = [NSSet setWithArray: @[@"sea",@"rch"]];

   // 支援Search

   userActivity.eligibleForSearch = YES;

   //送出設定

   [userActivity becomeCurrent];

在下面的函數中,我們可以處理使用者點選搜尋後的回調:

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:

{

NSString *activityType = userActivity.activityType;

   if ([activityType isEqual: @"myapp"]){

       // Handle restoration for values provided in userInfo

       // do something

       return YES;

   }

   return NO;

   //處理回調

}

TIP:這種方式添加的關鍵字搜尋,必須建立全局變量,否則無法進行搜尋:

2、CoreSpotlight

       CoreSpotlight是一種更加自由的搜尋方式,可以通過添加類似item的模型,将app中的資料展示在搜尋欄中,CoreSpotlight架構類似提供了一些增、删、改、查的操作,可是使我們自由的進行搜尋屬性的設定。

(1)認識3個類

在iOS9中,新增加了3個類,通過對這三個類的操作與配合,我們可以輕易的在app中添加CoreSpotlight搜尋的功能。

CSSearchableItemAttributeSet:設定類,這個類用于設定搜尋标簽裡的icon,内容,圖檔等。主要用法如下:

//這個類的核心方法隻有一個init方法,通過一個類型字元串進行建立,字元串用于在回調中區分

@interface CSSearchableItemAttributeSet : NSObject <NSCopying,NSSecureCoding>

- (instancetype)initWithItemContentType:(nonnull NSString *)itemContentType;

@end

//更多的屬性設定在其擴充類中,例如:

@interface CSSearchableItemAttributeSet (CSGeneral)

//展示的名稱

@property(nullable, copy) NSString *displayName;

//名稱數組

@property(nullable, copy) NSArray<NSString*> *alternateNames;

//完整的路徑

@property(nullable, copy) NSString *path;

//連結url

@property(nullable, strong) NSURL *contentURL;

//圖檔連結的url

@property(nullable, strong) NSURL *thumbnailURL;

//設定圖檔資料

@property(nullable, copy) NSData *thumbnailData;

//設定一個辨別符

@property(nullable, copy) NSString *relatedUniqueIdentifier;

@property(nullable, strong) NSDate *metadataModificationDate;

//内容類型

@property(nullable, copy) NSString *contentType;

@property(nullable, copy) NSArray<NSString*> *contentTypeTree;

//搜尋的關鍵字數組

@property(nullable, copy) NSArray<NSString*> *keywords;

//标題資訊

@property(nullable, copy) NSString *title;

CSSearchableItem:搜尋标簽類,通過這個類,來建立響應的搜尋标簽。主要内容如下:

//這個類主要用于建立搜尋的标簽

@interface CSSearchableItem : NSObject <NSSecureCoding, NSCopying>

//init方法

- (instancetype)initWithUniqueIdentifier:(nullable NSString *)uniqueIdentifier //Can be null, one will be generated

                       domainIdentifier:(nullable NSString *)domainIdentifier

                           attributeSet:(CSSearchableItemAttributeSet *)attributeSet;

//相應 的屬性

@property (copy) NSString *uniqueIdentifier;

@property (copy, nullable) NSString *domainIdentifier;

@property (copy, null_resettable) NSDate * expirationDate;

@property (strong) CSSearchableItemAttributeSet *attributeSet;

CSSearchableIndex:這個類,我個人了解,類似一個manager的作用,通過它對标簽進行增、删、改、查等操作:

@interface CSSearchableIndex : NSObject

@property (weak,nullable) id<CSSearchableIndexDelegate> indexDelegate;

//判斷裝置是否支援

+ (BOOL)isIndexingAvailable;

//取系統的searchIndex管理者

+ (instancetype)defaultSearchableIndex;

//一般情況下,我們不需要重新建立對象

- (instancetype)initWithName:(NSString *)name;

- (instancetype)initWithName:(NSString *)name protectionClass:(nullable NSString *)protectionClass;

//設定索引标簽

- (void)indexSearchableItems:(NSArray<CSSearchableItem *> *)items completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;

//删除指定id索引标簽

- (void)deleteSearchableItemsWithIdentifiers:(NSArray<NSString *> *)identifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;

- (void)deleteSearchableItemsWithDomainIdentifiers:(NSArray<NSString *> *)domainIdentifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;

//删除所有索引标簽

- (void)deleteAllSearchableItemsWithCompletionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;

繼續閱讀