发布于 2016-01-02 01:48:56 | 263 次阅读 | 评论: 0 | 来源: 分享

这里有新鲜出炉的精品教程,程序狗速度看过来!

iOS苹果移动操作系统

苹果iOS是由苹果公司开发的移动操作系统。苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计给iPhone使用的,后来陆续套用到iPod touch、iPad以及Apple TV等产品上。


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.在相应的视图控制器中引入头文件,然后就开始写代码使自己的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数组进行关联。具体代码和步骤如下。


  - (void)supportSpotlightSearch {
      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
      dispatch_async(queue, ^{
          <a href="http://www.jobbole.com/members/xyz937134366" data-mce-href="http://www.jobbole.com/members/xyz937134366">@try</a> {
              NSArray *temp = @[@"宫崎骏-龙猫", @"宫崎骏-千与千寻", @"宫崎骏-天空之城"];

              //创建SearchableItems的数组
              NSMutableArray *searchableItems = [[NSMutableArray alloc] initWithCapacity:temp.count];

             for (int i = 0; i ) {

                 //1.创建条目的属性集合
                 CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*) kUTTypeImage];

                 //2.给属性集合添加属性
                 attributeSet.title = temp[i];
                 attributeSet.contentDescription = [NSString stringWithFormat:@"宫崎骏与%@", temp[i]];
                 attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+1]]);

                 //3.属性集合与条目进行关联
                 CSSearchableItem *searchableItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%d", i+1] domainIdentifier:@"ZeluLi.SpotlightSearchDemo" attributeSet:attributeSet];

                 //把该条目进行暂存
                 [searchableItems addObject:searchableItem];
             }

             //4.吧条目数组与索引进行关联
             [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
                 if (!error) {
                     NSLog(@"%s, %@", __FUNCTION__, [error localizedDescription]);
                 }
             }];
         }
         <a href="http://www.jobbole.com/members/wx895846013" data-mce-href="http://www.jobbole.com/members/wx895846013">@catch</a> (NSException *exception) {
             NSLog(@"%s, %@", __FUNCTION__, exception);
         }
         <a href="http://www.jobbole.com/members/finally" data-mce-href="http://www.jobbole.com/members/finally">@finally</a> {

         }
     });
 }

3.处理搜索后条目点击的事件,该事件的处理要在AppDelegate中下面的委托代理方法中进行处理。下面的idetifier就是属性集合与条目进行关联时指定的唯一标示。


  - (BOOL)application:(nonnull UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler{

      NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];

      UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

      ViewController *vc = [navigationController viewControllers][0];
      [vc.myImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",idetifier]]];

     return YES;
 }

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务