国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS9開放的新API--Spotlight使用指南

2019-10-21 18:56:58
字體:
來源:轉載
供稿:網友
作為蘋果iOS9的重要特性之一,Spotlight搜索如今重新回到主界面最左側(同樣支持主界面下滑呼出),通過API的支持,還帶來了全新的Universal Search通用搜索功能,除了網絡以及系統本身內容之外,還能直接搜索第三方應用內的相關內容。下面我們就來詳細研究下Spotlight
 

1.Spotloight是什么?

  Spotlight在iOS9上做了一些新的改進, 也就是開放了一些新的API, 通過Core Spotlight Framework你可以在你的app中集成Spotlight。集成Spotlight的App可以在Spotlight中搜索App的內容,并且通過內容打開相關頁面。

  Demo演示

  iOS9開放的新API--Spotlight使用指南

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來判斷是哪個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;                   // 關鍵字,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以上設備運行,對與低版本的設備需加入這個防止崩潰問題

 



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 分宜县| 建阳市| 清水河县| 北京市| 泾源县| 石泉县| 五河县| 雅安市| 江陵县| 五家渠市| 安徽省| 会同县| 巴楚县| 萝北县| 玛沁县| 金乡县| 乌拉特前旗| 广元市| 皮山县| 屏东县| 新泰市| 札达县| 烟台市| 句容市| 康定县| 绥江县| 利津县| 三都| 松溪县| 嘉义市| 佛冈县| 三原县| 鄱阳县| 白银市| 茂名市| 宿迁市| 乌拉特后旗| 新巴尔虎右旗| 泸定县| 长寿区| 云安县|