iOS Search API - NSUserActivity

作者: 船长_ | 来源:发表于2016-01-04 23:43 被阅读2530次
  • NSUserActivity类是iOS8用来实现handoff的, iOS9中提供的新方法帮助你索引用户活动, 导航点. 它即支持私有内容, 也支持功能内容
  • 下面展示通过NSUserActivity类来索引一个APP里的内容
demo.gif

代码示例分析

一.准备工作:

  • 1.删除系统自带的storyboard,添加一个tableViewController,并且嵌套一个导航控制器;
  • 2.更改ViewController继承UITableViewController,并把ViewController与storyboard关联在一起
  • 3.创建三个继承于UIViewController控制器

二.在ViewController中

2.1添加activity

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addUserActivity];
    self.navigationItem.title = @"主界面";
}
- (void)addUserActivity{
    _activity = [[NSUserActivity alloc]initWithActivityType:@"chuanzhang"];
    _activity.title = @"Eden";
    
    _activity.keywords = [NSSet setWithArray:@[@"Adam", @"Lilith", @"Eve"]];
    _activity.eligibleForHandoff = NO;
    _activity.eligibleForSearch = YES;
    // 每个控制器的user activity和 搜索结果都是仅当应用曾经被打开过时而创建的
    // _activity.eligibleForPublicIndexing = YES;
    [_activity becomeCurrent];
}

2.2数据展示

#pragma mark UITableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"Adam";
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"Lilith";
    }else if (indexPath.row == 2){
        cell.textLabel.text = @"Eve";
    }
    return cell;
}

2.3监听cell的点击,实现控制器之间的跳转

#pragma mark UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        AdamViewController *adamView = [[AdamViewController alloc]init];
        [self.navigationController pushViewController:adamView animated:YES];
    }else if(indexPath.row == 1){
        LilithViewController *lilithView = [[LilithViewController alloc]init];
        [self.navigationController pushViewController:lilithView animated:YES];
    }else{
        EveViewController *eveView = [[EveViewController alloc]init];
        [self.navigationController pushViewController:eveView animated:YES];
    }
}

三.监听在Spotlight中的点击

3.1在AppDelegate方法中监听Spotlight中的点击

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
    UINavigationController *navi = (UINavigationController *)self.window.rootViewController;
    [navi.topViewController restoreUserActivityState:userActivity];
    
    return YES;
}

3.2在ViewController中实现跳转

- (void)restoreUserActivityState:(NSUserActivity *)activity{
    if ([activity.title isEqualToString:@"Adam"]) {
        AdamViewController *adamView = [[AdamViewController alloc]init];
        [self.navigationController pushViewController:adamView animated:YES];
    }else if([activity.title isEqualToString:@"Lilith"]){
        LilithViewController *lilithView = [[LilithViewController alloc]init];
        [self.navigationController pushViewController:lilithView animated:YES];
    }else  if([activity.title isEqualToString:@"Eve"]){
        EveViewController *eveView = [[EveViewController alloc]init];
        [self.navigationController pushViewController:eveView animated:YES];
    }
}

四.其他三个控制器

由于代码一样,只写一份参考

@interface LilithViewController ()
// 注意:必须是强引用
@property (nonatomic, strong)NSUserActivity *activity;
@end

@implementation LilithViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"Lilith";
    [self addUserActivity];

}
- (void)addUserActivity{
    
    // 使用唯一标识符 @"lilith"创建一个新的NSUserActivity对象
    // 这个工程已经被配置成确保使用这个标识符时要保证它不会被改变
    _activity = [[NSUserActivity alloc]initWithActivityType:@"lilith"];
    
    // 这就是将会在Spotlight 搜索结果里出现的内容
    _activity.title = @"Lilith";
    
    // 搜索的关键字
    // 为了确保可搜寻的内容不仅止限于应用的标题,你也要提供一系列的关键字
    _activity.keywords = [NSSet setWithArray:@[@"Lilith"]];
    
    // 是否将用户活动转交到其他设备
    _activity.eligibleForHandoff = NO;
    
    // 是否显示历史搜索记录
    _activity.eligibleForSearch = YES;
    
    // 它自动的被加入到了设备的搜索结果索引中
    [_activity becomeCurrent];
    
    // 过期时间,比如新闻一周后就过期
    // _activity.expirationDate =
    
    // 每个控制器的user activity和 搜索结果都是仅当应用曾经被打开过时而创建的
    // _activity.eligibleForPublicIndexing = YES;
}

相关文章

网友评论

  • dedenc:你好 我想问下 这个搜索的关键字又没有数量限制,怎么可以通过服务器动态的改变一下,适时改变搜索的关键字,或者说搜索的内容大部分和自己的app关联上,我看微博就是基本上搜索什么都会有他们。 希望能指导下 谢谢
    船长_:@dedenc 没有研究过,不宜过多吧
    dedenc:@船长_ 谢谢你的回复 我还有个小问题想问下, 这个关键字有没有个数或者数量的限制 他们是不是存储在自己的app里面 ,还是存储在整个手机里 , 如是我经常更新关键词 ,会不会造成存储量过大 被系统清理掉或者其他原因 。 非常感谢
    船长_:在后台请求到数据后,遍历关键字,参考下面代码
    ```
    // 保存到sportlight
    - (void)savePeopleToIndex {

    // 设置搜索属性
    NSMutableArray *searchableItems = [NSMutableArray array];
    for (FTFRewardUserList *p in self.resumeArray) {

    CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"image"];
    attributeSet.title = p.userName;
    attributeSet.contentDescription = [NSString stringWithFormat:@"%@", p.job];
    attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:@"AppIcon"]);

    CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%zd",[p.ID integerValue]] domainIdentifier:@"chuanzhang" attributeSet:attributeSet];
    [searchableItems addObject:item];
    }

    // 保存,将信息一项一项存入CoreSpotlight,以便用户搜索,显示搜索结果
    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
    if (error) {
    NSLog(@"error message:%@", error.localizedDescription);
    }
    }];
    }
    ```
  • helinyu:知道个大概,3Q
  • 闭家锁:很好,学习一下?

本文标题:iOS Search API - NSUserActivity

本文链接:https://www.haomeiwen.com/subject/uphohttx.html