美文网首页
iOS9 spotlight搜索 简单使用

iOS9 spotlight搜索 简单使用

作者: 一本大书 | 来源:发表于2018-06-21 12:43 被阅读129次

为什么要加这个功能

根据产品的性质、用户习惯,提高整个产品的体验与曝光率。

  • 用户习惯:就以我为例子,我的手机内装有上百来个app,每次要找应用我都通过spotlight直接输入应用名,这个时候输入关键字,相关的内容会出现在里边,试想,如果你能猜到用户的需求,在你的应用内加入相关的关键字,这里就会出现你的产品!增加了曝光度,刚好内容又吸引了用户的注意,用户点了,流量来了!
  • 产品:如果你的产品是社交类的app,那么在spotlight加上好友的相关信息,聊天记录作为关键字,如果用户这个时候想联系到这个人,在spotlight中输入好友姓名即可显示,这点可以参考下QQ的做法。

实现效果

实现效果

功能说明

调用iOS9提供的接口将数据的存入系统中,之后在spotlight中搜索就会出现你录入的数据。

    CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"Package"];
    attributeSet.title = @"test-title";
    //显示的描述
    attributeSet.contentDescription = @"test-description";
    //搜索关键字
//    attributeSet.keywords = @[title, @"CX"];
    //显示的图标
    UIImage *icon = [UIImage imageNamed:@"icon"];
    attributeSet.thumbnailData = UIImageJPEGRepresentation(icon, 1);

    CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"demo_id"
                                                               domainIdentifier:@"domainIdentifier"
                                                                   attributeSet:attributeSet];
    NSArray *items = @[item];
    
    CSSearchableIndex *searchableIndex = [CSSearchableIndex defaultSearchableIndex];
    [searchableIndex indexSearchableItems:items completionHandler:^(NSError * _Nullable error) {
        if (error != nil) {//添加索引失败
            NSLog(@"%@",[error localizedDescription]);
        }else{//成功
            NSLog(@"indexing successful");
        }
    }];

用户在spotlight中点击item后,调用AppDelegate如下方法

- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
    NSString *activityType = userActivity.activityType; // com.apple.corespotlightitem
    if ([activityType isEqualToString:@"com.apple.corespotlightitem"]) {
        NSString *uniqueId = userActivity.userInfo[CSSearchableItemActivityIdentifier];
        NSLog(@"uniqueId: %@", uniqueId);
    }
    return YES;
}

注意

  • CSSearchableItem 可以设置过期时间
// 默认是一个月,如果有需求,可以设置一下。
@property (copy, null_resettable) NSDate * expirationDate;
  • 如果你的应用支持iOS9以下的系统,在使用以上接口的时候,注意添加iOS系统版本号的判断,避免iOS9以下的设备闪退。

Demo

https://github.com/Linzehua2015/Spotlight

相关文章

  • iOS9 spotlight搜索 简单使用

    为什么要加这个功能 根据产品的性质、用户习惯,提高整个产品的体验与曝光率。 用户习惯:就以我为例子,我的手机内装有...

  • iOS9 SpotLight新功能(1)

    iOS9推出后,为了顺应时代,给自己项目加了个Spotlight搜索功能,下面是使用过程中的一点记录 在iOS9中...

  • iOS9 Day-by-Day:: Day 1 :: Searc

    原文地址 在 iOS9 之前,你只能在 spotlight 中输入关键字,比如应用名字,搜索到应用。在 iOS9 ...

  • iOS9 Day-by-Day:: Day 1 :: Searc

    在 iOS9 之前,你只能在 spotlight 中输入关键字,比如应用名字,搜索到应用。在 iOS9 中苹果提供...

  • iOS9-新特性之Core Spotlight

    iOS9中的Spotlight和Safari搜索可以直接搜到App内部的内容(注意不是app),Apple提供了让...

  • iOS应用内搜索CoreSpotlight使用

    点击下载 Demo 一、Core Spotlight 简介 iOS9 推出了 Core Spotlight 框架,...

  • Spotlight

    何为App Search?在iOS9之前,用户通过spotlight只能搜索app的名字,或者苹果自带app的一些...

  • Mac效率神器Alfred

    Alfred是和spotlight同类型但更强大的效率软件。以前会使用spotlight来切换APP、搜索文件等,...

  • Mac 工具推荐

    随手写写。。。 非程序员工具 Spotlight 系统自带: 神器中的战斗机Spotlight不仅能简单的搜索文字...

  • iOS9:快速让你的APP支持spotlight搜索

    iOS9中支持为app中的内容做索引以支持spotlight搜索,感觉是个很有心意的功能。需要提到的是这些索引是存...

网友评论

      本文标题:iOS9 spotlight搜索 简单使用

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