Core Spotlight和深度连接结合使用(上)

在iOS 9.0以前,Apple Spotlight仅可以检索iOS自身应用的内容,好比邮件、备忘录、提醒、短信。第三方应用不支持被检索,好比美团、大众点评、今日头条等等。在iOS9.0以后,iOS苹果推出Search API,使得第三方APP内的页面内容也能够被检索。应用开发者按照Search API编程,而后用户在Spotlight和Safari能够直接搜APP内的内容(In-App Search),这带来很大的价值点。git

据WWDC官方公布的用户习惯数据,用户86%的时间花在APP中,仅有14%的时间花在 Web上。因此APP有着较好的用户体验很是重要。github

对APP开发者而言:
最大价值是提升APP的打开率,从而提升了APP的留存及活跃,提升APP的曝光度,用户可以更加方便的达到内容。编程

对用户而言:
对于安装不少APP的用户,找个某一个APP,都特别困难。用Spotlight输入APP的名字,即可找到。用户在Spotlight也可以方便查找大众点评中的某一个餐厅。服务器

Spotlight给咱们提供了这样好的功能,应用开发者怎样使用呢?微信

**app

iOS 9 Search API概述dom

**ide

•A private on-device index(私有设备索引)。保存在用户设备上,不会同步到服务器与其它设备上。网站

•Apple’s server-side index (Apple server索引)。pubulic index,内容保存在应用服务器。spa

**

Search API的三种用法

**

NSUserActivity

这个类咱们很熟悉在iOS 8的时候,咱们就拿它来作Handoff,在iOS 9中咱们能拿它来作更多的事儿了~在这里咱们先说它的搜索功能,当内容被记NSUserActivity,就能够在 Spotlight 和 Safari 中同时被搜索到,如今这里咱们只介绍建立用户索引。

Core Spotlight
iOS 9中全新提出的Api,容许App在本地存一个相似索引的文件,能够曾删改查,用来搜索本地内容(on-device index)。适合持续的用户数据。

Web markup。
网站上的内容如何在App中存在能够在搜索显示App相关信息,pubulic index.内容必须在应用服务器,苹果经过applebot获取相关数据,iOS全部用户都可以利用Spotligight和Safari搜索功能获取到相关内容。(国内不支持)

**

为App添加Spotlight支持

**

新建了一个Demo工程作例子演示,最后会提供Demo下载地址

-(IBAction)creatSearchableItem{

CSSearchableItemAttributeSet注:Spotlight只支持iOS 9+若是你的项目支持iOS 9如下版本须要添加以下方法判断
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000

//code...

#endif

第一步:导入Framework

MobileCoreServices.framework
CoreSpotlight.framework

第二步:导入头文件

import <CoreSpotlight/CoreSpotlight.h>

import <MobileCoreServices/MobileCoreServices.h>

第三步:建立Spotlight索引

*attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
    // 标题
   attributeSet.title = @"标题";
    // 关键字,NSArray可设置多个
    attributeSet.keywords = @[@"demo",@"sp"];
    // 描述
   attributeSet.contentDescription = @"description";
    // 图标, NSData格式
    attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:@"icon"]);
    // Searchable item
   CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"1" domainIdentifier:@"linkedme.cc" attributeSet:attributeSet];
    NSMutableArray *searchItems = [NSMutableArray arrayWithObjects:item, nil];
    //indexSearchableItems 接收参数NSMutableArray
   [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchItems completionHandler:^(NSError * error) {
        if (error) {
           NSLog(@"索引建立失败:%@",error.localizedDescription);
        }else{
            [self performSelectorOnMainThread:@selector(showAlert:) withObject:@"索引建立成功" waitUntilDone:NO];

        }
    }];
}

CSSearchableItemAttributeSet设置Spotlight搜索内容的类,咱们能够设置各类属性以下图

方法声明

- (instancetype)initWithUniqueIdentifier:(NSString *)uniqueIdentifier 
                        domainIdentifier:(NSString *)domainIdentifier 
                            attributeSet:(CSSearchableItemAttributeSet *)attributeSet;

参数详解

图片描述

查看官方文档

经过上面的操做咱们已经能够在Spotlight中搜索到咱们建立的索引内容了,能够搜索到了下一步就是怎么经过搜索内容打开相应的页面.

经过搜索结果跳转到相应页面
在Appdelegate中添加下面方法

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

      NSString* idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];        //获取传入的索引数据的惟一标识
   if ([idetifier isEqualToString:@"1"]) {
       DemoOneViewController * ovc = [[DemoOneViewController alloc]init];
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
       [navigationController pushViewController: ovc animated:true];
    }
    NSLog(@"%@",idetifier);
    return YES;
}

同时Spotlight还提供删除索引方法,过时的索引须要手动删除,系统提供了三个删除索引方法

经过identifier删除索引

- (IBAction)deleteSearchableItemFormIdentifier{
   [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:@[@"1"] completionHandler:^(NSError * _Nullable error) {
       if (error) {
            NSLog(@"%@", error.localizedDescription);
        }else{
            [self performSelectorOnMainThread:@selector(showAlert:) withObject:@"经过identifier删除索引成功" waitUntilDone:NO];
        }
    }];
}

经过DomainIdentifiers删除索引

- (IBAction)deleteSearchableItemFormDomain{
    [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@[@"linkedme.cc"] completionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error.localizedDescription);
        }else{
            [self performSelectorOnMainThread:@selector(showAlert:) withObject:@"经过DomainIdentifiers删除索引成功" waitUntilDone:NO];
        }
    }];
}

删除全部索引

- (IBAction)deleteAllSearchableItem{
    [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@",error.localizedDescription);
        }else{
            [self performSelectorOnMainThread:@selector(showAlert:) withObject:@"删除全部索引成功" waitUntilDone:NO];
        }
    }];

由以上步骤,移动开发者在开发APP时,能够集成Spotlight功能,可是在编程时,会遇到各类各样的坑。集成Spotlight功能能够和深度连接结合,将大大下降开发成本,加强的深度连接也引导从渠道(微信、微博、短信、邮件等)上一键唤醒APP。Spotlight和深度连接将怎样更好的融合呢。请见《Core Spotlight和深度连接结合使用(下)》

下载Demo
参考链接LinkedME

相关文章
相关标签/搜索