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; // 標題
attributeSet.keywords = keywords; // 關(guān)鍵字,NSArray格式
attributeSet.contentDescription = spotlightDesc; // 描述
attributeSet.thumbnailData = photo; // 圖標, 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; // 標題
attributeSet.keywords = keywords; // 關(guān)鍵字,NSArray格式
attributeSet.contentDescription = spotlightDesc; // 描述
attributeSet.thumbnailData = photo; // 圖標, 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 App開發(fā)中的UIPageControl分頁控件使用小結(jié)
UIPageControl分頁控件的例子簡單來說即是我們平時翻動多個桌面頁時及底部帶有的圓點頁碼標注,這里我們來看一下iOS App開發(fā)中的UIPageControl分頁控件使用小結(jié),需要的朋友可以參考下2016-06-06iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南
這里我們要介紹的是AVAudio ToolBox框架中的AudioServicesPlaySystemSound函數(shù)創(chuàng)建的服務(wù),特別適合用來制作鈴聲,下面就簡單整理一下iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南:2016-06-06iOS開發(fā)教程之識別圖片中二維碼功能的實現(xiàn)
長按識別二維碼這個功能相信對大家來說都不陌生,最近工作中就遇到了這個需求,所以下面這篇文章主要給大家介紹了關(guān)于利用iOS識別圖片中二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-07-07