系统级搜索(基础应用)-CSSearchable

来源:互联网 发布:c语言 贪吃蛇 编辑:程序博客网 时间:2024/05/20 20:17

上周末听了“飞雀教育-张诚老师”的公开课,把学到的东西一起整理下发上来,任君采摘 o.o (任君采摘呦~)。

系统级搜索,用起来感觉很潮,可以让用户在系统搜索框中搜索到我们应用中的东西,从而增加应用的出镜率。

如图:(第一张是正常搜索框,后者是运行过APP后的搜索框)




已下是代码已经写好注释了呦~ 

    //创建姓名

    NSArray*nameArray=@[@"xxx测试"];

    

   //创建特性  用于记录内容

    CSSearchableItemAttributeSet*attributeset=[[CSSearchableItemAttributeSetalloc]initWithItemContentType:@"iamge"];

    //设置相关属性

    attributeset.title=nameArray[0];

    //设置

    attributeset.thumbnailData=UIImagePNGRepresentation([UIImageimageNamed:@"1.png"]);


   //具体的描述信息

    attributeset.contentDescription=[NSStringstringWithFormat:@"描述的标签%@",nameArray[0]];


    

    //属性设置完了以后我们需要把他加入到item

    

    CSSearchableItem*item=[[CSSearchableItemalloc]initWithUniqueIdentifier:nameArray[0]domainIdentifier:@"com.ios9.searchDemo"attributeSet:attributeset];

    

   //item加入到系统级搜索内容里面

    [[CSSearchableIndexdefaultSearchableIndex]indexSearchableItems:@[item]completionHandler:^(NSError *_Nullable error) {

//回调

        NSLog(@"%@",error);

    }];


到此运行后就能看到图2中搜索的效果了。


添加唤醒事件---在搜索中找到了相应地信息,点击信息,然后唤醒APP,与正常唤醒一样在AppDelegate中写下如下方法

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

{

   //唤醒时读取出的内容,注:内容为一个字典需要什么参数请打印字典内容。

    

    NSLog(@"%@",userActivity.userInfo);

    

    returnYES;

}


最后的最后,如果你发现有什么错误,或者有什么更好的方法,请留言告诉我哦,你的代码比你想象中的强大。

打完收工~

感谢观看,学以致用更感谢!



0 0
原创粉丝点击