iOS中Core Spotlight開發(fā)

  iOS隨著CoreSpotlight框架的引入,app將支持Spotlight搜索應用中的數(shù)據(jù),并快速進入app中進行相關操作。

先看效果圖:

Simulator Screen Shot 2016年4月14日 下午4.51.39.png

開始開發(fā):
1、引入CoreSpotlight和MobileCoreServices

屏幕快照 2016-04-14 下午4.56.27.png
#import <CoreSpotlight/CoreSpotlight.h>
#impor <MobileCoreServices/MobileCoreServices.h>

2、保存數(shù)據(jù)

//創(chuàng)建SearchableItems的數(shù)組
NSMutableArray *searchableItems = [[NSMutableArray alloc] init];
//1.創(chuàng)建條目的屬性集合
CSSearchableItemAttributeSet
* attributeSet = [[CSSearchableItemAttributeSet alloc]
initWithItemContentType:(NSString*)kUTTypeImage];
//2.給屬性集合添加屬性
attributeSet.title = title;
attributeSet.contentDescription = desc;
attributeSet.thumbnailData = UIImagePNGRepresentation(image);
//3.屬性集合與條目進行關聯(lián)
CSSearchableItem
*searchableItem = [[CSSearchableItem alloc]
initWithUniqueIdentifier:identifier domainIdentifier:domainIdentifier
attributeSet:attributeSet];
//把該條目進行暫存
[searchableItems addObject:searchableItem];
//4.把條目數(shù)組與索引進行關聯(lián)
[[CSSearchableIndex
defaultSearchableIndex] indexSearchableItems:searchableItems
completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"save spotilight error %s, %@", __FUNCTION__, [error localizedDescription]);
}
}];

3、刪除相應數(shù)據(jù)

[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:identifiers completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"del spotilight error %s, %@", __FUNCTION__, [error localizedDescription]);
}
}];
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:domainIdentifiers completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"del domain spotilight error %s, %@", __FUNCTION__, [error localizedDescription]);
}
}];
[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"del all spotilight error %s, %@", __FUNCTION__, [error localizedDescription]);
}
}];

4、Spotlight搜索點擊跳轉

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];
return YES;
}

最后,運行Demo:https://github.com/iOSXH/iOSTests

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,926評論 25 709
  • 風花雪月喝不醉 第二天,璀璨改了機票,他和明亮去了寂照庵。寂照庵在蒼山囯家森林公園的最上面,明亮討厭爬山。還不到一...
    陶之夭夭1閱讀 522評論 0 2
  • 帶著我的小粗腿到處尋找有趣有意思有記憶的地方。 穿著我的五彩衣服四處發(fā)現(xiàn)充滿意思充滿花色充滿生活的地方。 生活的煙...
    哈巴巴PLUS閱讀 444評論 2 2

友情鏈接更多精彩內容