iOS開發之CoreSpotlight框架的應用
阿新 • • 發佈:2019-02-25
iOS開發之CoreSpotlight框架的應用
CoreSpotlight是iOS提供的一套本地檢索推薦功能。開發者可以為自己的應用新增本地索引,使用者通過索引中定義的關鍵字可以搜尋並定位到應用程式內的指定功能。
一、一個簡單的新增索引示例
- (void)viewDidLoad { [super viewDidLoad]; //建立索引屬性物件 CSSearchableItemAttributeSet *set = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*)kUTTypeText]; //設定索引屬性 set.title = @"哈哈哈"; set.displayName = @"Hello"; set.alternateNames = @[@"aaa",@"bbb"]; set.keywords = @[@"333",@"444"]; set.version = @"1.1"; set.path = @"path"; set.thumbnailURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"]]; //建立索引 CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"1111" domainIdentifier:@"huishao" attributeSet:set]; //新增索引 [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"buildSearchableItem Error:%@",error.localizedDescription); } }]; }
在搜尋欄中搜索索引的關鍵字,標題,名稱、路徑都可以搜尋到當前應用程式。例如:
二、CSSearchableIndex索引管理類
CSSearchableIndex類提供了對索引的操作功能,例如新增索引,查詢索引,刪除索引等等,解析如下:
//代理物件 @property (weak,nullable) id<CSSearchableIndexDelegate> indexDelegate; //獲取索引檢索是否可用 + (BOOL)isIndexingAvailable; //獲取預設提供的索引管理物件 + (instancetype)defaultSearchableIndex; //建立一個索引管理物件 - (instancetype)initWithName:(NSString *)name; - (instancetype)initWithName:(NSString *)name protectionClass:(nullable NSFileProtectionType)protectionClass; //獲取索引管理物件中的所有索引 - (void)indexSearchableItems:(NSArray<CSSearchableItem *> *)items completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler; //通過識別符號來刪除索引 - (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;
三、CSSearchableIndexDelegate詳解
CSSearchableIndexDelegate提供了索引查詢的相關回調方法,解析如下:
//這個代理重新索引所有可搜尋的資料,並且清除任何本地狀態(可能該狀態已經被持久化),因為索引已經丟失了 - (void)searchableIndex:(CSSearchableIndex *)searchableIndex reindexAllSearchableItemsWithAcknowledgementHandler:(void (^)(void))acknowledgementHandler; //根據id重新索引所有可搜尋的資料 - (void)searchableIndex:(CSSearchableIndex *)searchableIndex reindexSearchableItemsWithIdentifiers:(NSArray <NSString *> *)identifiers acknowledgementHandler:(void (^)(void))acknowledgementHandler; //已經進入節能模式呼叫的方法 - (void)searchableIndexDidThrottle:(CSSearchableIndex *)searchableIndex; // 結束節能模式呼叫的方法 - (void)searchableIndexDidFinishThrottle:(CSSearchableIndex *)searchableIndex; //用來提供資料 - (nullable NSData *)dataForSearchableIndex:(CSSearchableIndex *)searchableIndex itemIdentifier:(NSString *)itemIdentifier typeIdentifier:(NSString *)typeIdentifier error:(out NSError ** __nullable)outError; //用來提供檔案地址 - (nullable NSURL *)fileURLForSearchableIndex:(CSSearchableIndex *)searchableIndex itemIdentifier:(NSString *)itemIdentifier typeIdentifier:(NSString *)typeIdentifier inPlace:(BOOL)inPlace error:(out NSError ** __nullable)outError;
四、CSSearchableItem索引類
CSSearchableItem用來進行索引的定義,解析如下:
//通過設定唯一識別符號、域名和屬性來定義索引
- (instancetype)initWithUniqueIdentifier:(nullable NSString *)uniqueIdentifier
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;
五、CSSearchableItemAttributeSet類
這個類的主要作用是進行索引資訊的配置,CSSearchableItemAttributeSet並沒定義太多的屬性,CoreSpotlight中提供了這個類的大量Category來補充這個類的功能,解析如下:
//---------------標準---------------------------------
//通過型別建立一個索引資訊類
- (instancetype)initWithItemContentType:(nonnull NSString *)itemContentType;
//設定索引的展示名稱 可以進行搜尋
@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, strong) NSDate *metadataModificationDate;
//內容型別
@property(nullable, copy) NSString *contentType;
//關鍵字陣列
@property(nullable, copy) NSArray<NSString*> *keywords;
//設定標題
@property(nullable, copy) NSString *title;
//設定版本
@property(nullable, copy) NSString *version;
//設定搜尋權重 0-100之間
@property(nullable, strong) NSNumber *rankingHint;
//域名標識
@property(nullable, copy) NSString *domainIdentifier;
//------------動作相關--------------------------------
//是否支援撥打電話 必須先設定phoneNumbers屬性
@property(nullable, strong) NSNumber *supportsPhoneCall;
//是否支援導航 必須先設定經緯度資訊
@property(nullable, strong) NSNumber *supportsNavigation;
//------------容器相關--------------------------------
//設定容器標題
@property(nullable, copy) NSString *containerTitle;
//設定容器顯示名
@property(nullable, copy) NSString *containerDisplayName;
//設定容器標識
@property(nullable, copy) NSString *containerIdentifier;
//設定容器順序
@property(nullable, strong) NSNumber *containerOrder;
//-----------圖片相關-------------------------------
//圖片高度
@property(nullable, strong) NSNumber *pixelHeight;
//圖片高度
@property(nullable, strong) NSNumber *pixelWidth;
//畫素總數
@property(nullable, strong) NSNumber *pixelCount;
//顏色空間
@property(nullable, copy) NSString *colorSpace;
//每一幀的bit數
@property(nullable, strong) NSNumber *bitsPerSample;
//拍照時是否開啟閃光燈
@property(nullable, strong, getter=isFlashOn) NSNumber *flashOn;
//焦距是否35毫米
@property(nullable, strong, getter=isFocalLength35mm) NSNumber *focalLength35mm;
//裝置製造商資訊
@property(nullable, copy) NSString *acquisitionMake;
//裝置模型資訊
@property(nullable, copy) NSString *acquisitionModel;
//... 更多圖片資訊
//----------媒體相關-------------------------------
//編輯者
@property(nullable, copy) NSArray<NSString*> *editors;
//下載時間
@property(nullable, strong) NSDate *downloadedDate;
//檔案描述
@property(nullable, copy) NSString *comment;
//內容版權
@property(nullable, copy) NSString *copyright;
//最後使用時間
@property(nullable, strong) NSDate *lastUsedDate;
//新增時間
@property(nullable, strong) NSDate *addedDate;
//時長
@property(nullable, strong) NSNumber *duration;
//聯絡人關鍵字
@property(nullable, copy) NSArray<NSString*> *contactKeywords;
//媒體型別
@property(nullable, copy) NSArray<NSString*> *mediaTypes;
//總位元率
@property(nullable, strong) NSNumber *totalBitRate;
//視訊比特幣
@property(nullable, strong) NSNumber *videoBitRate;
//音訊位元率
@property(nullable, strong) NSNumber *audioBitRate;
//組織資訊
@property(nullable, copy) NSArray<NSString*> *organizations;
//建立者
@property(nullable, copy) NSString *role;
//語言資訊
@property(nullable, copy) NSArray<NSString*> *languages;
//釋出者
@property(nullable, copy) NSArray<NSString*> *publishers;
//地址
@property(nullable, strong) NSURL *URL;
//... 更多媒體資訊
//---------------資訊相關---------------------------
//網頁資料
@property(nullable, copy) NSData *HTMLContentData;
//文字資料
@property(nullable, copy) NSString *textContent;
//作者資料
@property(nullable, copy) NSArray<CSPerson*> *authors;
//郵箱地址
@property(nullable, copy) NSArray<NSString*> *authorEmailAddresses;
@property(nullable, copy) NSArray<NSString*> *emailAddresses;
//電話號碼
@property(nullable, copy) NSArray<NSString*> *phoneNumbers;
//... 更多
//--------------地址相關-------------------
//郵編
@property(nullable, copy) NSString *postalCode;
//城市
@property(nullable, copy) NSString *city;
//國家
@property(nullable, copy) NSString *country;
//海拔
@property(nullable, strong) NSNumber *altitude;
//經度
@property(nullable, strong) NSNumber *latitude;
//緯度
@property(nullable, strong) NSNumber *longitude;
//速度
@property(nullable, strong) NSNumber *speed;
//時間戳
@property(nullable, strong) NSDate *timestamp;
//... 更多