CoreSpotlight是iOS提供的一套本地檢索推薦功能。開發者能夠爲本身的應用添加本地索引,用戶經過索引中定義的關鍵字能夠搜索並定位到應用程序內的指定功能。git
- (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類提供了對索引的操做功能,例如添加索引,查找索引,刪除索引等等,解析以下:框架
//代理對象 @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提供了索引查找的相關回調方法,解析以下:dom
//這個代理從新索引全部可搜索的數據,而且清除任何本地狀態(可能該狀態已經被持久化),由於索引已經丟失了 - (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用來進行索引的定義,解析以下:ide
//經過設置惟一標識符、域名和屬性來定義索引 - (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並沒定義太多的屬性,CoreSpotlight中提供了這個類的大量Category來補充這個類的功能,解析以下:post
//---------------標準--------------------------------- //經過類型建立一個索引信息類 - (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; //... 更多