iOS9開放的新API--Spotlight使用指南
1.Spotloight是什么?
Spotlight在iOS9上做了一些新的改進, 也就是開放了一些新的API, 通過Core Spotlight Framework你可以在你的app中集成Spotlight。集成Spotlight的App可以在Spotlight中搜索App的內(nèi)容,并且通過內(nèi)容打開相關(guān)頁面。
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以下設(shè)備運行崩潰的問題
b.創(chuàng)建CSSearchableItemAttributeSet 對象
CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
attributeSet.title = spotlightTitle; // 標(biāo)題
attributeSet.keywords = keywords; // 關(guān)鍵字,NSArray格式
attributeSet.contentDescription = spotlightDesc; // 描述
attributeSet.thumbnailData = photo; // 圖標(biāo), NSData格式
// 把圖片轉(zhuǎn)換成NSData的方法
UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]
c.創(chuàng)建可檢索條目CSSearchableItem
// spotlightInfo 可以作為一些數(shù)據(jù)傳遞給接受的地方
// domainId id,通過這個id來判斷是哪個spotlight
CSSearchableItem *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; // 標(biāo)題
attributeSet.keywords = keywords; // 關(guān)鍵字,NSArray格式
attributeSet.contentDescription = spotlightDesc; // 描述
attributeSet.thumbnailData = photo; // 圖標(biāo), NSData格式
// spotlightInfo 可以作為一些數(shù)據(jù)傳遞給接受的地方
// 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);
}
}];
}
========加載本地圖片的使用方法========
========加載網(wǎng)絡(luò)圖片的使用方法========
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@" [self insertSearchableItem:data spotlightTitle:@"等風(fēng)來" description:@"等風(fēng)來描述" 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后的響應(yīng)方法========
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
if ([[userActivity activityType] isEqualToString:CSSearchableItemActionType]) {
NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier];
// 接受事先定義好的數(shù)值,如果是多個參數(shù)可以使用把json轉(zhuǎn)成string傳遞過來,接受后把string在轉(zhuǎn)換為json
NSLog(@"傳遞過來的值%@", uniqueIdentifier);
}
return YES;
}
備注:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
// 相關(guān)spotlight的方法等
#endif
// Spotlight支持iOS9以上設(shè)備運行,對與低版本的設(shè)備需加入這個防止崩潰問題
相關(guān)文章
IOS開發(fā)中禁止NavigationController的向右滑動返回
這篇文章主要介紹了IOS開發(fā)中禁止NavigationController的向右滑動返回的相關(guān)資料,需要的朋友可以參考下2017-03-03
iOS開發(fā)存儲應(yīng)用程序Info.plist知識全面詳解
這篇文章主要為大家介紹了iOS開發(fā)存儲應(yīng)用程序Info.plist知識全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06

