您的当前位置:首页正文

iOS9 App Search之NSUserActivity与C

来源:要发发知识网

最近因为项目要求,开始研究起iOS9推出的feature--App Search APIs。何为App Search?在iOS9之前,用户通过spotlight只能搜索app的名字,或者苹果自带app的一些内容,比如搜索通讯录、备忘录中的内容。iOS9开始,用户可以通过spotlight搜索网页内容或者任何app内的数据。

1、search APIs主要包括三部分

1.1、NSUserActivity

NSUserActivity在iOS8中就出现了,当时用于Handoff。
在iOS9中,我们可以把想要在spotlight中搜出来的内容放到NSUserActivity中。userActivityUIResponser的属性,通常会在用户访问某一个页面时,对UIViewControlleruserActivity属性赋值。
NSUserActivity可以用来对历史访问记录

1.2、Core Spotlight

通过该技术,用户可以通过spotlight搜到app中曾经出现过或者现有的所有内容。

1.3、Web Markup

2、NSUserActivity

2.1、创建NSUSerActivity实例

在viewModel层中创建的NSUSerActivity实例

- (NSUserActivity *)serviceProjectUserActivity
{
    if (!_serviceProjectUserActivity) {
        _serviceProjectUserActivity = [[NSUserActivity alloc] initWithActivityType:@"com.xxxx.appIdentifier.serviceProject"];
        _serviceProjectUserActivity.title = self.projectEntity.projectTitle ?: @"";
        _serviceProjectUserActivity.userInfo = @{@"id" : self.projectEntity.projectId ?: @""};
        _serviceProjectUserActivity.keywords = [NSSet setWithObjects:self.projectEntity.destination ?: @"", nil];
        // 好像并没有什么卵用,不知道哪里用错了
        _serviceProjectUserActivity.expirationDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 *31];
        // 如果没有显示设置为yes,则不可搜索到
        _serviceProjectUserActivity.eligibleForSearch = YES;
        //
        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:CFBridgingRelease(kUTTypeContact)];
        attributeSet.contentDescription = self.projectEntity.features ?: @"";
        // ??????网络路径不可显示
        attributeSet.thumbnailURL = [NSURL URLWithString:self.projectEntity.backImgUrl ?: @""];
        // 防止NSUserActivity和Core Spotlight可能重复索引,这里设置为nil
        attributeSet.relatedUniqueIdentifier = nil;
        _serviceProjectUserActivity.contentAttributeSet = attributeSet;
    }
    return _serviceProjectUserActivity;
}

2.2、赋值给UIViewController的userActivity属性

在viewController层监测,如果页面数据请求完成,给userActivity属性赋值

 __weak typeof(self)weakSelf = self;
    [RACObserve(self.viewModel, projectEntity) subscribeNext:^(id x) {
        if (x) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            strongSelf.userActivity = strongSelf.viewModel.serviceProjectUserActivity;
        }
    }];

2.3、在Appdelegate中处理页面跳转

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
    if ([userActivity.activityType isEqualToString:@"com.xxxx.appIdentifier.serviceProject"]) {
        NSString *projectID = userActivity.userInfo[@"id"];
        if (projectID.length > 0) {
            // 处理具体页面跳转
            return YES;
        }
        return NO;
    }
    return NO;
}

3、Core Spotlight

3.1、创建要搜索的项,并将所有的项加入默认索引空间

- (void)indexAllDomesticCityForAppSearch
{
    // 把国内一级城市从数据库中拿出来
    NSString *sql = [NSString stringWithFormat:@"select cn, zone_id from %@ where rank == '1' and zone_id <= 900000", self.tableName];
    
    [self loadDestinationWithSQL:sql andResultSetHandler:^(FMResultSet *result) {
        NSMutableArray *searchableItems = [[NSMutableArray alloc] init];
        while ([result next]) {
            NSString *zoneName = [result stringForColumnIndex:0];
            NSString *zoneID = [result stringForColumnIndex:1];
           // 创建对应的CSSearchableItem
            CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:kUTTypeContent];
            attributeSet.title = zoneName;
            attributeSet.contentDescription = @"专业旅游";
            // 这个属性主要是将NSUserActivity与Core Spotlight indexed object进行一个关联,防止出现重复的内容(如果出现重复内容,是因为开始的时候测试NSUserActivity的时候没有设置id,还原一下模拟器就好了)
            attributeSet.relatedUniqueIdentifier = zoneID;
            CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:zoneID domainIdentifier:@"com.xxxx.appIndetifier.destinations" attributeSet:attributeSet];
            [searchableItems addObject:item];
        }
         // 所有的items加入索引
        CSSearchableIndex *defaultSearchableIndex = [CSSearchableIndex defaultSearchableIndex];
        [defaultSearchableIndex indexSearchableItems:[searchableItems copy] completionHandler:^(NSError * _Nullable error) {
            
        }];
    }];
}

3.2、在Appdelegate中处理从spotlight打开app

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
    if ([userActivity.activityType isEqualToString:@"com.xxxx.appIndetifier.destinations"]) {
        NSString *zoneID = userActivity.userInfo[CSSearchableItemActivityIdentifier];
        if (zoneID.length > 0) {
            // 处理具体页面跳转
            return YES;
        }
        return NO;
    }
    return NO;
}