iOS 9 Spotlight搜尋 OC版,spotlightoc

來源:互聯網
上載者:User

iOS 9 Spotlight搜尋 OC版,spotlightoc

介紹
    在WWDC 2015會議上,蘋果官方公布了iOS9。除開許多新的特性和增強功能,這次升級也給了開發人員們一個機會讓他們的app裡的內容能通過Spotlight 搜尋功能被發現和使用。在iOS9中可用的新APIs允許你去索引APP裡面的內容或者介面狀態,通過Spotlight來讓使用者使用。 這些新的搜尋APIs的三大組件為:

  * NSUserActivity 類, 它是為可被看見的APP內容而設計的


  * Core Spotlight 架構, 為任何APP內容而設計的 


  * web markup,為這一類型的APP設計的,就是APP的內容在某個網站上有鏡像



在這個教程裡,我將會向你展示可以怎樣在你的應用中使用NSUserActivity類以及 Core Spotlight 架構。

準備工作
    這個教程需要你運行在Xcode7 和OSX 10.10、iOS9.0系統或更後的系統  

步驟

#import <CoreSpotlight/CoreSpotlight.h>

    2.建立搜尋屬性對象

 CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@""];

    3.設定搜尋屬性

//搜尋顯示的名稱            attributeSet.title = obj.name;            //顯示的描述            attributeSet.contentDescription = obj.desc;            //搜尋索引鍵            attributeSet.keywords = @[obj.name,@"CX"];            //顯示的表徵圖            UIImage * icon = [UIImage imageNamed:obj.imageName];            if (icon) {                    attributeSet.thumbnailData = UIImageJPEGRepresentation(icon, 1);            }

 
    4.根據搜尋屬性建立搜尋對象(domainIdentifier:唯一標識)
       

CSSearchableItem * item = [[CSSearchableItem alloc] initWithUniqueIdentifier:obj.name domainIdentifier:SearchDomain attributeSet:attributeSet];

    5.將搜尋對象添加到搜尋數組
       

[searchItems addObject:item];

    6.設定索引目錄
  

 CSSearchableIndex * searchableIndex = [CSSearchableIndex defaultSearchableIndex];    [searchableIndex indexSearchableItems:searchItems completionHandler:^(NSError * _Nullable error) {        if (error != nil) {//添加索引失敗            NSLog(@"%@",[error localizedDescription]);        }else{//成功            NSLog(@"indexing successful");        }    }];

 
    7.實現AppDelegate方法(使用者通過spotlight搜尋到APP裡面的內容  點擊內容進入APP  就會調用這個方法)
       

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{            UINavigationController * vc = (UINavigationController *)self.window.rootViewController;                    [vc.topViewController restoreUserActivityState:userActivity];                    return YES;        }

 


    8.在搜尋列表控制器實現方法(activity裡面有使用者點擊spotlight搜尋列表中某條資料的所有屬性   根據屬性做相應的操作)
      

 - (void)restoreUserActivityState:(NSUserActivity *)activity{}

 

代碼地址

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.