iOS中的搜索

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Q52077987/article/details/82827856

NSUserActivity

NSUserActivity主要用于HandOff,简单说就是切换设备能接着做之前的事情,比如在手机浏览网页,打开Mac可以直接打开手机浏览的那个网页并且停留在阅读位置。因此NSUserActivity主要是用来记录应用程序当前状态的。在iOS9以后也支持在Spotlight中搜索。如果纯粹想索引一些内容,还是应该使用Core Spotlig。稍后会给出二者的比较。

NSUserActivity用法

假设数据是这样的:

SongInfo *song1 = [[SongInfo alloc] init];
song1.song = @"吻别";
song1.album = @"吻别";
song1.singer = @"张学友";

SongInfo *song2 = [[SongInfo alloc] init];
song2.song = @"今天";
song2.album = @"真永远";
song2.singer = @"刘德华";
self.songs = @[song1, song2];

创建一个NSUserActivity,设置索引关键字,然后赋值给UIViewController的userActivity属性。userActivity属性是UIViewController继承自UIResponse的,因此所有继承自UIResponse的类都可以使用。

NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@"com.lanyue.UserActivityTest"];

activity.title = self.song.song;
activity.keywords = [[NSSet alloc] initWithObjects:self.song.song, self.song.singer, self.song.album, nil];

activity.eligibleForHandoff = NO;
activity.eligibleForSearch = YES;
activity.eligibleForPublicIndexing = NO;

activity.needsSave = YES;
activity.delegate = self;

self.userActivity = activity;

将userActivity属性赋值后,UIKit会自动保存当前的状态,在保存之前会回调delegate的userActivityWillSave方法,通常在这个方法中设置userInfo。userInfo中保存的内容并不会显示在搜索结果中,而是用于应用状态恢复。对于建立索引不是必须的。

#pragma mark - NSUserActivityDelegate
-(void)userActivityWillSave:(NSUserActivity *)userActivity{
    userActivity.userInfo = @{@"song": self.song.song};
}

当点击搜索结果启动App的时候,会调用AppDelegate中的continueUserActivity方法,通过这个方法可以决定是恢复应用到之前的状态(比如App已经在搜索的界面了,或者搜索的内容不存在了,可以选择不跳转界面)。至于如何恢复状态,就是开发者的逻辑了。

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
    UINavigationController *mainVC = (UINavigationController *)application.keyWindow.rootViewController;
    if([mainVC.viewControllers count] != 0){
        ViewController *vc = (ViewController *) mainVC.viewControllers[0];
        [vc resotreUserActivitiyState: userActivity];
    }

    return YES;
}

如何在非UIResponse的类中使用NSUserActivity呢?

1.需要强引用一个NSUserActivity对象,系统创建索引是异步的,在未完成之前,不能释放NSUserActivity对象。

2.调用[activity becomeCurrent];主动触发索引。

Core Spotlight

Spotlight使用

创建CSSearchableItem。需要先创建一个CSSearchableItemAttributeSet,然后用这个attributeSet再创建Item。

扫描二维码关注公众号,回复: 3525844 查看本文章
CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"data"];

attributeSet.title = @"songs";
attributeSet.contentDescription = @"iWork Numbers Document";
attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:@"1.png"]);

CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"1234" domainIdentifier:@"4567" attributeSet:attributeSet];

CSSearchableItemAttributeSet中有一个方法,setValue:forCustomKey。允许用户设置自定义的属性。比如:

CSCustomAttributeKey *myKey = [[CSCustomAttributeKey alloc] initWithKeyName:@"myyyyKey" searchable:YES searchableByDefault:YES unique:YES multiValued:NO];
[attributeSet setValue:@"hhhhh" forCustomKey:myKey];

添加这个Key之后,就可以通过hhhhh来搜索App。

点击搜索结果启动应用时,处理过程和NSUserActivity一样,也是回调continueUserActivity这个方法。

NSUserActivity和Core Spotlight比较

NSUserActivity用于索引用户看过的东西,就像一个浏览器对用户浏览历史的管理。其设计目的就是让用户能够搜寻之前看过的页面和跨设备继续浏览(Hand Off)。NSUserActivity还有一个特性:如果很多使用同一款App的用户索引了同一个页面,即使新用户没有浏览过,也可以在Spotlight中搜索到(需要指定eligibleForPublicIndexing为YES)。

Core Spotlight是真正面向搜索的,内容比较详尽,根据官方文档介绍,一个App所以最好在几千个之内。Core Spotlight这套API创建的索引不能跨设备。

The Core Spotlight APIs do not make items publicly searchable. Instead, Core Spotlight enables you to make items searchable in the user’s private, on-device index, the contents of which is never shared with Apple or synced between devices

Mark Up Web Content

iOS的Spotlight搜索还能够显示Web上的内容,如果有App的内容有对应的H5页面,可以让H5页面的内容被Spotlight搜索到,即使用户没有安装App也能看到。Mark Up Web Content和Universe Link技术结合,可以成为一种推广App的渠道。

如果想让苹果索引你的页面,最直接的方法就是在提交App的时候,填写support URL或者Marketing Website

在这里插入图片描述

需要注意的是,要修改网站的robots.txt文件允许Apple的爬虫爬取网站,Apple的爬虫叫做Applebot 。用这个工具来测试网站的配置是否正确。

如何添加索引

添加索引是H5端的工作,在内容页面添加meta信息,Applebot就会索引网页内容,下边是官网示例:

<title>Beats by Dr. Dre Solo2 Wireless Headphones - Apple</title>
<meta property="og:description" content="Beats by Dr. Dre Solo2 Wireless Headphones let you take your music anywhere you go. Get fast, free shipping when you buy online.">
<span itemprop="reviewCount">924</span>
<meta itemprop="ratingValue" content="4.5">
<meta itemprop="priceCurrency" content="USD">

在这里插入图片描述
在Spotlight中点击了搜索的内容,如果用户没有安装App,将打开Safari显示对应的H5页面。

H5页面和App的结合,可以使用Smart Banner + Universal Link技术,Smart Banner在Web页面显示一个App Store的超链接,邀请用户下载App。Universal Link作用是,对于一个URL,如果用户没有安装App,将打开Safari显示H5,如果用户安装了App,将打开App跳转到内容页面。(当然,也需要App客户端做解析URL和跳转的工作)

WWDC

App Search Programming Guide

猜你喜欢

转载自blog.csdn.net/Q52077987/article/details/82827856