zoukankan      html  css  js  c++  java
  • iOS开发之集成iOS9中的Core Spotlight Framework搜索App的内容

      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#//apple_ref/doc/uid/TP40016250

      一.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

  • 相关阅读:
    Python36和Python27共存的方法
    普通用户启动redis
    NetHogs 实时检测网络流量 转
    Linux命令nohup+screen 转
    变更Linux下的Java版本 alternatives
    centos7下使用yum安装mysql
    CentOS6.6系统中安装配置Samba的教程
    Linux服务器中木马(肉鸡)手工清除方法(转)
    linux杀毒软件clamav安装与使用
    msys git 安装配置、git命令行使用
  • 原文地址:https://www.cnblogs.com/ludashi/p/4867511.html
Copyright © 2011-2022 走看看