iOS9 Spotlight使用,ios9spotlight使用
1.Spotloight是什麼?
Spotlight在iOS9上做了一些新的改進, 也就是開放了一些新的API, 通過Core Spotlight Framework你可以在你的app中整合Spotlight。整合Spotlight的App可以在Spotlight中搜尋App的內容,並且通過內容開啟相關頁面。
Demo示範
2.如何整合Spotlight
a.添加所需要的架構
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000#import <CoreSpotlight/CoreSpotlight.h>#import <MobileCoreServices/MobileCoreServices.h>#endif
注,很多APP都是支援iOS9以下的,因此加入#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000,可以解決iOS9以下裝置運行崩潰的問題
b.建立 CSSearchableItemAttributeSet 對象
CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage]; attributeSet.title = spotlightTitle; // 標題 attributeSet.keywords = keywords; // 關鍵字,NSArray格式 attributeSet.contentDescription = spotlightDesc; // 描述 attributeSet.thumbnailData = photo; // 表徵圖, NSData格式
// 把圖片轉換成NSData的方法
UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]
c.建立可檢索條目CSSearchableItem
// spotlightInfo 可以作為一些資料傳遞給接受的地方// domainId id,通過這個id來判斷是哪個spotlightCSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:spotlightInfo domainIdentifier:domainId attributeSet:attributeSet];
d.添加檢索入口
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * error) { if (error) { NSLog(@"indexSearchableItems Error:%@",error.localizedDescription); } }];
========完整代碼========
- (void)insertSearchableItem:(NSData *)photo spotlightTitle:(NSString *)spotlightTitle description:(NSString *)spotlightDesc keywords:(NSArray *)keywords spotlightInfo:(NSString *)spotlightInfo domainId:(NSString *)domainId { CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage]; attributeSet.title = spotlightTitle; // 標題 attributeSet.keywords = keywords; // 關鍵字,NSArray格式 attributeSet.contentDescription = spotlightDesc; // 描述 attributeSet.thumbnailData = photo; // 表徵圖, NSData格式 // spotlightInfo 可以作為一些資料傳遞給接受的地方 // domainId id,通過這個id來判斷是哪個spotlight CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:spotlightInfo domainIdentifier:domainId attributeSet:attributeSet]; [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * error) { if (error) { NSLog(@"indexSearchableItems Error:%@",error.localizedDescription); } }];}
========載入本地圖片的使用方法========
[self insertSearchableItem:UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]) spotlightTitle:@"等風來" description:@"等風來描述" keywords:@[@"鮑鯨鯨",@"大麗花"] spotlightInfo:@"傳遞過去的值" domainId:@"com.wb.spotlight"];
========載入網狀圖片的使用方法========
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://hiphotos.baidu.com/doc/pic/item/eaf81a4c510fd9f905f61934262dd42a2934a48e.jpg"]]; [self insertSearchableItem:data spotlightTitle:@"等風來" description:@"等風來描述" keywords:@[@"鮑鯨鯨",@"大麗花"] spotlightInfo:@"傳遞過去的值" domainId:@"com.wb.spotlight"]; });
========刪除所有spotlight的方法========
[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"%@", error.localizedDescription); } }];
========刪除指定的spotlight的方法========
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@"domainId" completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"%@", error.localizedDescription); } }];
========點擊spotlight後的回應程式法========
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler { if ([[userActivity activityType] isEqualToString:CSSearchableItemActionType]) { NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier]; // 接受事先定義好的數值,如果是多個參數可以使用把json轉成string傳遞過來,接受後把string在轉換為json NSLog(@"傳遞過來的值%@", uniqueIdentifier); } return YES;}
備忘:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
// 相關spotlight的方法等
#endif
// Spotlight支援iOS9以上裝置運行,對與低版本的裝置需加入這個防止崩潰問題
如果你不是在wb145230部落格園看到本文,請點擊查看原文.