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

首頁 > 學院 > 開發設計 > 正文

iOS開發之集成iOS9中的CoreSpotlightFramework搜索App的內容

2019-11-14 18:07:53
字體:
來源:轉載
供稿:網友

  Spotlight在iOS9上做了一些新的改進, 也就是開放了一些新的API, 通過Core Spotlight Framework你可以在你的app中集成Spotlight。集成Spotlight的App可以在Spotlight中搜索App的內容,并且通過內容打開相關頁面。因為接到開發任務,老大說讓在App中支持Spotlight, 于是又搞了搞蘋果的官方文檔。可以說,集成Spotlight不算復雜,官網上講的也挺明白的,今天博客就通過一個Demo來集成一下Spotlight。

  蘋果官方有關Core Spotlight Framework的鏈接如下:

  https://developer.apple.com/library/PRerelease/ios/documentation/CoreSpotlight/Reference/CoreSpotlight_Framework/index.html

  一.Demo運行效果

  還是通過一個Demo來進行介紹,Demo運行效果如下。我們App中有關于宮崎駿的的內容,然后在Spotlight中搜索宮崎駿,就可以搜索到相關內容,并且可以點擊打開展示相關內容。具體運行效果如下:

  二.集成Core Spotlight Framework

    1.想在App中使用Spotlight,首先得引入Core Spotlight Framework,Targets ->General -> linked Frameworks and Libraries 點擊加號添加CoreSpotlight.framework。如下截圖所示。

 

    2.在相應的視圖控制器中引入<CoreSpotlight/CoreSpotlight.h>頭文件,然后就開始寫代碼使自己的App內容支持Spotlight搜索了。下面是為Demo添加Spotlight的相關代碼。Spotlight搜索出來的東西,每一項就是一個條目即CSSearchableItem的對象,而改對象又關聯一個屬性集合(CSSearchableItemAttributeSet )該集合中存儲了CSSearchableItem對象的相關屬性,如果title(標題), contentDescription(內容簡介),

thumbnailData(圖片)等所需內容。具體請看下方代碼描述和代碼注釋。

    代碼描述:

      (1).首先定義了一個temp數組,用來存儲在Spotlight中搜索的關鍵字,也就是Spotlight可以搜索到的App內容。數組中的內容通過循環遍歷經過一系列的步驟給Spotlight進行關聯。

      (2)在每次遍歷內容數組的過程中,需要創建一個CSSearchableItemAttributeSet(屬性集合),并給屬性集合中的一些屬性賦上值。然后再創建一個CSSearchableItem,創建CSSearchableItem時,把其對應的屬性集合進行關聯。把每次創建好的條目暫存到可變數組中,因為創建好所有的條目后還要和Spotlight的索引(CSSearchableIndex)進行關聯。

      (3)通過單例獲取CSSearchableIndex的對象,并與我們創建好的CSSearchableItem數組進行關聯。具體代碼和步驟如下。

 1 - (void)supportSpotlightSearch { 2     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 3     dispatch_async(queue, ^{ 4         @try { 5             NSArray *temp = @[@"宮崎駿-龍貓", @"宮崎駿-千與千尋", @"宮崎駿-天空之城"]; 6              7             //創建SearchableItems的數組 8             NSMutableArray *searchableItems = [[NSMutableArray alloc] initWithCapacity:temp.count]; 9             10             for (int i = 0; i < temp.count; i ++) {11                 12                 //1.創建條目的屬性集合13                 CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*) kUTTypeImage];14                 15                 //2.給屬性集合添加屬性16                 attributeSet.title = temp[i];17                 attributeSet.contentDescription = [NSString stringWithFormat:@"宮崎駿與%@", temp[i]];18                 attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+1]]);19                 20                 //3.屬性集合與條目進行關聯21                 CSSearchableItem *searchableItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%d", i+1] domainIdentifier:@"ZeluLi.SpotlightSearchDemo" attributeSet:attributeSet];22                 23                 //把該條目進行暫存24                 [searchableItems addObject:searchableItem];25             }26             27             //4.吧條目數組與索引進行關聯28             [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {29                 if (!error) {30                     NSLog(@"%s, %@", __FUNCTION__, [error localizedDescription]);31                 }32             }];33         }34         @catch (NSException *exception) {35             NSLog(@"%s, %@", __FUNCTION__, exception);36         }37         @finally {38             39         }40     });41 }

 

    3.處理搜索后條目點擊的事件,該事件的處理要在AppDelegate中下面的委托代理方法中進行處理。下面的idetifier就是屬性集合與條目進行關聯時指定的唯一標示。

 1 - (BOOL)application:(nonnull UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler{ 2      3     NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"]; 4      5     UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; 6      7     ViewController *vc = [navigationController viewControllers][0]; 8     [vc.myImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",idetifier]]]; 9     10     11     return YES;12 }

 

    DEMO分享地址--github:https://github.com/lizelu/SpotlightSearchDemo


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福鼎市| 正宁县| 红河县| 三明市| 名山县| 仙游县| 容城县| 蕲春县| 延边| 永春县| 静安区| 忻城县| 阳泉市| 福州市| 小金县| 贞丰县| 中山市| 浦北县| 桑植县| 大田县| 武功县| 渭南市| 宣化县| 青田县| 水富县| 五莲县| 荔波县| 新巴尔虎左旗| 延长县| 阿尔山市| 澄迈县| 蓝山县| 莱州市| 高密市| 突泉县| 中宁县| 瑞金市| 麦盖提县| 卓尼县| 石城县| 竹山县|