iOS9新特性 - Search APIs

本人录制技术视频地址:https://edu.csdn.net/lecturer/1899 欢迎观看。

在IOS9之前,你只能用spotlight通过app的名字找到对应的app。但随着iOS9  Search APIs 的发布,开发者可以通过在app内部的内容中建立索引,通过搜索关键字来进入到app指定的内容区域。

The 3 APIs

NSUserActivity

The NSUserActivity 在iOS8的 Handoff 使用中有介绍到。到了iOS9中,你可以利用NSUserActivity搜索相关的“活跃信息”。你可以提供一些关键词给这些“活跃信息”, 意味着spotlight可以检索它们。这个操作相当于你在浏览网页的时候,有历史记录的作用一样。用户可以通过spotlight快速的打开最近的“活跃信息”。

Web Markup

Web Markup 允许 apps 映射它们的内容到网页上面,然后方便spotlight在网页中快速检索内容。Apple的索引器将会扮演爬虫的角色,在网页上面检索那些被Markup的内容。这些信息在Safari和Spotlight都可以检索。

Core Spotlight

Core Spotlight是iOS9中的一个新框架,它允许检索在app中的内容。NSUserActivity在保存用户的历史记录是很有用的;而Core Spotlight可以检索任何你想要的数据。

使用Core Spotlight APIs

NSUserActivity 和 Web Markup APIs 相对而言是很容易使用的,而 Core Spotlight 是有一点复杂的。为了演示Core Spotlight是怎样工作的,我们创建一个比较简单的Demo用来展示朋友列表。然后你点击任意一个朋友的名字,你可以看到朋友的头像的具体信息。在演示具体的流程之前,我们先看看最终效果图。 

在演示效果中可以看到,我在spotlight中检索相关朋友信息,可以看到他们的大致信息,然后点击一条信息,便可以跳转到自己app中的具体的朋友详情。



代码分析:

1. Demo的结构很简单,就是一个导航控制器,根控制器是一个UITableViewController,用来展示朋友名称列表。我们定义为FriendTableViewController。

2. 当点击列表中的一个朋友名称后,就会跳转到详情页面,我们定义为FriendViewController。

3. 所有的朋友数据信息,我们用一个管理类来管理。我们定义为DataSource。这个管理类的职责:

1)存储所有的朋友数据信息。

2)保存用户信息到Core Spotlight的索引器中。

当然,每一条用户信息对应一个模型,定义如下:

Person.h

@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *identifer;
@property (nonatomic, copy) NSString *icon;
- (instancetype)initWithName:(NSString *)name identifer:(NSString *)identifer icon:(NSString *)icon;
@end
Person.m

@implementation Person
- (instancetype)initWithName:(NSString *)name identifer:(NSString *)identifer icon:(NSString *)icon {
    if (self = [super init]) {
        self.name = name;
        self.identifer = identifer;
        self.icon = icon;
    }
    return self;
}
@end

DataSource.h的方法列表如下:

@interface Datasource : NSObject
- (NSArray *)dataList;
- (Person *)findFriendWithId:(NSString *)identifer;
- (void)savePeopleToIndex;
@end

1)dataList方法就是获取所有的用户列表数据信息。

2)findFriendWithId: 方法就是根据用户Id获取模型数据。

3)savePeopleToIndex就是保存所有用户数据信息到Core Spotlight的索引器中。

DataSource.m 文件中的代码

@implementation Datasource

- (NSArray *)dataList {
    Person *becky = [[Person alloc] initWithName:@"Becky" identifer:@"1" icon:@"becky"];
    
    Person *ben = [[Person alloc] initWithName:@"Ben" identifer:@"2" icon:@"ben"];

    Person *jane = [[Person alloc] initWithName:@"Jane" identifer:@"3" icon:@"jane"];

    Person *pete = [[Person alloc] initWithName:@"Pete" identifer:@"4" icon:@"pete"];

    Person *ray = [[Person alloc] initWithName:@"Ray" identifer:@"5" icon:@"ray"];

    Person *tom = [[Person alloc] initWithName:@"Tom" identifer:@"6" icon:@"tom"];
    
    return @[becky, ben, jane, pete, ray, tom];
}

- (Person *)findFriendWithId:(NSString *)identifer {
    for (Person *p in self.dataList) {
        if ([p.identifer isEqualToString:identifer]) {
            return p;
        }
    }
    return nil;
}

- (void)savePeopleToIndex {
    // prepare
    NSMutableArray *searchableItems = [NSMutableArray array];
    for (Person *p in self.dataList) {
        // Create an attribute set for an item that represents an image.
        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"image"];
        attributeSet.title = p.name;
        attributeSet.contentDescription = [NSString stringWithFormat:@"This is an entry all about the interesting person called %@", p.name];
        attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:p.icon]);
        
        CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:p.identifer domainIdentifier:@"com.ios9daybyday.SearchAPIs.people" attributeSet:attributeSet];
        [searchableItems addObject:item];
    }
    
    // save
    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"error message:%@", error.localizedDescription);
        }
    }];
}

@end
代码的关键部分就是savePeopleToIndex方法,定义的searchableItems就是用来存储相关的可检索的信息;而代码中的CSSearchableIndex的单例方法indexSearchableItems是真正的将searchableItems中的内容存储到Core Spotlight中的操作。

然后我们看看FriendTableViewController的列表展示页面的主要代码

- (void)viewDidLoad {
    [super viewDidLoad];
    Datasource *dataSource = [Datasource alloc];
    self.dataList = [dataSource dataList];
    
    [dataSource savePeopleToIndex];
}
这里的savePeopleIndex就将内容存储到Core Spotlight中了。

现在你运行程序,这些数据将会被存储起来了。当你在spotlight中搜索你的朋友,他们将会出现,效果图如下:


此时尝试点击一项,但是它不会跳转到app的指定区域,只会跳转到对应的app,因为我们还没有指定要跳转的指定区域。

我们可以在调用continueUserActivity代理方法的时候指定app的行为。代码如下:

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler NS_AVAILABLE_IOS(8_0) {
    
    NSString *friendID = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    [navigationController popToRootViewControllerAnimated:NO];
    
    FriendTableViewController *friendVC = (FriendTableViewController *)navigationController.viewControllers.firstObject;
    [friendVC showFriendWithId:friendID];
    
    return YES;
}

第1句代码:       获取你在spotlight中点击的朋友的id。

第2、3句代码:    获取到根的导航控制,并且pop掉栈中所有的控制器。

第4句代码:          跳转到app的指定位置。

这里的代码可以可以发现,之前我们保存到Core SpotLight索引器中的内容现在可以使用userActivity.userInfo字典进行获取了。我们所关心的就是friend Id,它是被保存到索引器中作为Person这个对象的kCSSearchableItemActivityIdentifier.

正如你所见的,左上角有一个 "Back to search"选项,用户可以点击这里返回用户列表。

这篇文章中,并没有涉及到索引器的删除操作,想要了解删除的具体操作,可以参照以下的几个方法:

deleteSearchableItemsWithIdentifiers
deleteSearchableItemsWithDomainIdentifiers
deleteAllSearchableItemsWithCompletionHandler

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值