最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2015年11月14日 17:01:19   投稿:hebedich  
作為蘋果iOS9的重要特性之一,Spotlight搜索如今重新回到主界面最左側(cè)(同樣支持主界面下滑呼出),通過API的支持,還帶來了全新的Universal Search通用搜索功能,除了網(wǎng)絡(luò)以及系統(tǒng)本身內(nèi)容之外,還能直接搜索第三方應(yīng)用內(nèi)的相關(guān)內(nèi)容。下面我們就來詳細(xì)研究下Spotlight

1.Spotloight是什么?

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

  Demo演示

  

2.如何集成Spotlight

  a.添加所需要的框架 

復(fù)制代碼 代碼如下:

#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è)備運(yùn)行崩潰的問題

  b.創(chuàng)建CSSearchableItemAttributeSet 對(duì)象

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

// spotlightInfo 可以作為一些數(shù)據(jù)傳遞給接受的地方
// domainId      id,通過這個(gè)id來判斷是哪個(gè)spotlight
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:spotlightInfo domainIdentifier:domainId attributeSet:attributeSet];

  d.添加檢索入口

復(fù)制代碼 代碼如下:

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * error) {
     if (error) {
        NSLog(@"indexSearchableItems Error:%@",error.localizedDescription);
     }
}];

  ========完整代碼========

復(fù)制代碼 代碼如下:

- (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,通過這個(gè)id來判斷是哪個(gè)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);
          
        }
    }];
}

  ========加載本地圖片的使用方法========

復(fù)制代碼 代碼如下:
[self insertSearchableItem:UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]) spotlightTitle:@"等風(fēng)來" description:@"等風(fēng)來描述" keywords:@[@"鮑鯨鯨",@"大麗花"] spotlightInfo:@"傳遞過去的值" domainId:@"com.wb.spotlight"];

  ========加載網(wǎng)絡(luò)圖片的使用方法========

復(fù)制代碼 代碼如下:
 
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的方法========

復(fù)制代碼 代碼如下:

[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}
}];

  ========刪除指定的spotlight的方法========

復(fù)制代碼 代碼如下:

[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@"domainId" completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}
}];

  ========點(diǎn)擊spotlight后的響應(yīng)方法========

復(fù)制代碼 代碼如下:

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
if ([[userActivity activityType] isEqualToString:CSSearchableItemActionType]) {
NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier];
// 接受事先定義好的數(shù)值,如果是多個(gè)參數(shù)可以使用把json轉(zhuǎn)成string傳遞過來,接受后把string在轉(zhuǎn)換為json
NSLog(@"傳遞過來的值%@", uniqueIdentifier);
}
return YES;
}

  備注:

復(fù)制代碼 代碼如下:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
  // 相關(guān)spotlight的方法等
#endif

// Spotlight支持iOS9以上設(shè)備運(yùn)行,對(duì)與低版本的設(shè)備需加入這個(gè)防止崩潰問題

相關(guān)文章

最新評(píng)論

正阳县| 辉南县| 泸州市| 富宁县| 宁蒗| 巴马| 板桥市| 新宁县| 岚皋县| 青河县| 综艺| 清镇市| 竹北市| 凌源市| 澄江县| 南郑县| 桑植县| 赞皇县| 高邑县| 米易县| 营山县| 新巴尔虎右旗| 洛扎县| 福清市| 高陵县| 松阳县| 南丰县| 丰都县| 江西省| 依安县| 建平县| 邵阳县| 富锦市| 专栏| 安顺市| 海口市| 肥东县| 石景山区| 东港市| 莎车县| 郑州市|