iOS Search API - NSUserActivity

  • NSUserActivity类是iOS8用来实现handoff的, iOS9中提供的新方法帮助你索引用户活动, 导航点. 它即支持私有内容, 也支持功能内容
  • 下面展示通过NSUserActivity类来索引一个APP里的内容
iOS Search API - NSUserActivity_第1张图片
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;
}

你可能感兴趣的:(iOS Search API - NSUserActivity)