iOS 8 NSUserActivity和iOS9 CoreSpotlight 搜索API的使用

一 iOS 8 NSUserActivity 使用

可以在设备Spotlight用 通过APP名字和关键字搜索出应用,点击跳转APP

iOS 8 NSUserActivity和iOS9 CoreSpotlight 搜索API的使用_第1张图片

//NSUserActivity的注册
#pragma mark - userActivity
-(NSUserActivity *)userActivity{

   NSUserActivity * activity = [[NSUserActivity alloc]initWithActivityType:@"XXXX"];
   activity.title = @"XXXX"; // 搜索app显示的名字
   if (IS_IOS9) {
        activity.keywords = [NSSet setWithArray:@[@"xxx",@"xxx",@"xxxx",@"漏洞",@"厂商",@"liyiyi"]];
        activity.eligibleForHandoff =YES;// 打开 HandOff 功能
        activity.eligibleForSearch = YES; // 开启spotlight search 功能

    }
    [activity becomeCurrent];//开启 NSUserActivity
    return activity;
    //Handoff Overview
    //[_activity addUserInfoEntriesFromDictionary:@""];

}

appDelegate函数api

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
    return YES;
}

-(BOOL)application:(UIApplication *)application willContinueUserActivityWithType:(NSString *)userActivityType{
    return YES;
}

以上只是单纯的实现,搜索APP点击打开应用的功能。

二、下面显示 NSUserActivity和spotlight结合搜索到应用内的相关文章

导入头文件

iOS 8 NSUserActivity和iOS9 CoreSpotlight 搜索API的使用_第2张图片

#import 

在appDelegate里面完成spotlight的设置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{}

spotlight设置

+(void)spotlight:(NSArray *)coreArray{
 // coreArray 数据源

    if(IS_IOS9){

        NSMutableArray *searchTableItems=[NSMutableArray arrayWithCapacity:0];

        [coreArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 创建 item

            ItemsModel *itemModel= (ItemsModel *)obj;

            CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"text"];
            NSString *uniqueId=[NSString stringWithFormat:@"%@#$%@",itemModel.title,itemModel.link];// 此处在 appdelegate里面的
            attributeSet.title = expModel.title;
            attributeSet.contentDescription = [NSString stringWithFormat:NSLocalizedString(@"%@", nil),itemModel.excerpt];

            CSSearchableItem *item=[[CSSearchableItem alloc] initWithUniqueIdentifier:uniqueId domainIdentifier:@"com.xxx.xxxxxx" attributeSet:attributeSet];
            [searchTableItems addObject:item];
        }];

        [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchTableItems completionHandler:^(NSError * __nullable error) {

            if (!error)
                NSLog(@"%@",error.localizedDescription);
        }];
    }   
}

Universal Link 关联app的AppDelegate代理方法

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{

    NSLog(@"activityType  is %@",userActivity.activityType);
    NSLog(@"title  is %@",userActivity.title);
    NSLog(@"userInfo  is %@",userActivity.userInfo);
    NSLog(@"webpageURL  is %@",userActivity.webpageURL);
    NSLog(@"keywords  is %@",userActivity.keywords);
    NSLog(@"contentAttributeSet is %@",userActivity.contentAttributeSet.title);
    NSLog(@"contentAttributeSet is %@",userActivity.contentAttributeSet.subject);
    NSLog(@"contentAttributeSet is %@",userActivity.contentAttributeSet.theme);

    if([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]){
    // 跳转 app 方式
        NSURL *webUrl = userActivity.webpageURL;
        if ([webUrl.host isEqualToString:@""]) {
            //判断域名 打开对应页面


        }else{
        //不能识别 safrai 打开

            [[UIApplication sharedApplication] openURL:webUrl];

        }

    }else if ([userActivity.activityType isEqualToString:@"com.apple.corespotlightitem"]){
    // spotLight 方式

        NSString *userinfo=userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];
       // 根据 userInfo 实现相应操作

    }

    return YES;
}

-(BOOL)application:(UIApplication *)application willContinueUserActivityWithType:(NSString *)userActivityType{
    NSUserActivity * activity = [[NSUserActivity alloc]initWithActivityType:userActivityType];

    application.userActivity=activity;

    return YES;
}

打印userActivity的所有属性 发现点击搜索结果 位置有关联的属性是userInfo

@property (nullable, copy) NSDictionary *userInfo;

字典userInfo的键的值userInfo[@"kCSSearchableItemActivityIdentifier"]
是 创建

 CSSearchableItem *item=[[CSSearchableItem alloc] initWithUniqueIdentifier:uniqueId domainIdentifier:@"com.xxx.xxxxxx" attributeSet:attributeSet];

中的 uniqueid, 所有可以通过 uniqueId传入一些参数 或者协议 现实一些相应操作。

这个功能实现是在12月初现实的当时研究这个功能的时候觉得有好多要总结的,过了这么长时间一些细节和现实过程中的心得都忘记差不多了,只是粗狂的记得这些步骤了这里写图片描述

你可能感兴趣的:(ios,搜索,应用,app,ios)