iOS9 Spotlight使用,ios9spotlight使用

來源:互聯網
上載者:User

iOS9 Spotlight使用,ios9spotlight使用

1.Spotloight是什麼?

  Spotlight在iOS9上做了一些新的改進, 也就是開放了一些新的API, 通過Core Spotlight Framework你可以在你的app中整合Spotlight。整合Spotlight的App可以在Spotlight中搜尋App的內容,並且通過內容開啟相關頁面。

  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以下裝置運行崩潰的問題

  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來判斷是哪個spotlightCSSearchableItem *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以上裝置運行,對與低版本的裝置需加入這個防止崩潰問題

 

  如果你不是在wb145230部落格園看到本文,請點擊查看原文

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.