iOS9-新特性之Core Spotlight

  • iOS9中的Spotlight和Safari搜索可以直接搜到App内部的内容(注意不是app),Apple提供了让用户更容易找到他们想要的内容,哪怕这个内容是在某个App内部的,从而提高App的使用度和曝光度

  • 应用示例:

    • 腾讯新闻app
    • 简书app
    • 新浪微博app
  • 为了演示Core Spotlight是怎样工作的,我们创建一个比较简单的Demo用来展示朋友列表。然后你点击任意一个朋友的名字,你可以看到朋友的头像的具体信息。在演示具体的流程之前,我们先看看最终效果图。

demo.gif
  • 在演示效果中可以看到,我在spotlight中检索相关朋友信息,可以看到他们的大致信息,然后点击一条信息,便可以跳转到自己app中的具体的朋友详情(注意是具体详情界面,而腾讯新闻app则是打开app而已,并没有跳转到详情页面)。

代码分析:

一.程序的入口

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

    self.window = [[UIWindow alloc] init];
    FriendTableViewController *vc = [[FriendTableViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    return YES;
}

二.模型Person-每一条用户信息对应一个模型

Person.h声明:
@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *identifer;
@property (nonatomic, copy) NSString *icon;
- (instancetype)initWithName:(NSString *)name identifer:(NSString *)identifer icon:(NSString *)icon;

Person.m实现:
@implementation Person
- (instancetype)initWithName:(NSString *)name identifer:(NSString *)identifer icon:(NSString *)icon {
    if (self = [super init]) {
        self.name = name;
        self.identifer = identifer;
        self.icon = icon;
    }
    return self;
}
@end

三.所有的朋友数据信息,我们用一个管理类来管理。我们定义为DataSource。这个管理类的职责:

  • 1.存储所有的朋友数据信息。
  • 2.保存用户信息到Core Spotlight的索引器中。

DataSource.h方法列表

@interface DataSource : NSObject
// 获取所有的用户列表数据信息
- (NSArray *)dataList;

// 保存所有用户数据信息到Core Spotlight的索引器中
- (void)savePeopleToIndex;
@end

DataSource.m方法实现

  • 注意先将framework导入

工程->Build Phases->Link Binary With Libraries->搜索CoreSpotlight

// 导入头文件
#import <CoreSpotlight/CoreSpotlight.h>
@implementation DataSource
- (NSArray *)dataList {
    Person *becky = [[Person alloc] initWithName:@"Becky" identifer:@"1" icon:@"becky"];
    
    Person *ben = [[Person alloc] initWithName:@"Ben" identifer:@"2" icon:@"ben"];
    
    Person *jane = [[Person alloc] initWithName:@"Jane" identifer:@"3" icon:@"jane"];
    
    Person *pete = [[Person alloc] initWithName:@"Pete" identifer:@"4" icon:@"pete"];
    
    Person *ray = [[Person alloc] initWithName:@"Ray" identifer:@"5" icon:@"ray"];
    
    Person *tom = [[Person alloc] initWithName:@"Tom" identifer:@"6" icon:@"tom"];
    
    return @[becky, ben, jane, pete, ray, tom];
}


- (void)savePeopleToIndex {

    // 设置搜索属性
    NSMutableArray *searchableItems = [NSMutableArray array];
    for (Person *p in self.dataList) {

        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"image"];
        attributeSet.title = p.name;
        attributeSet.keywords = @[p.name];
        attributeSet.contentDescription = [NSString stringWithFormat:@"这个家伙名字叫 %@", p.name];
        attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:p.icon]);
        
        CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:p.identifer domainIdentifier:@"chuanzhang" attributeSet:attributeSet];
        // 过期的日期,默认过期的日期是一个月
        // item.expirationDate = ;
        [searchableItems addObject:item];
    }
    
    // 保存,将信息一项一项存入CoreSpotlight,以便用户搜索,显示搜索结果
    // 单例
    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"error message:%@", error.localizedDescription);
        }
    }];
}

代码的关键部分就是savePeopleToIndex方法,定义的searchableItems就是用来存储相关的可检索的信息;而代码中的CSSearchableIndex的单例方法indexSearchableItems是真正的将searchableItems中的内容存储到Core Spotlight中的操作

四.展示数据

4.1 FriendTableViewController.h方法声明

@class Person;
@interface FriendTableViewController : UITableViewController
// 根据用户Id获取模型数据
- (Person *)findFriendWithId:(NSString *)identifer;
@end

FriendTableViewController.m方法实现

这个比较简单,不解释了!

@interface FriendTableViewController ()
/*** 存放模型的数组 ***/
@property (nonatomic,strong) NSArray *dataList;

/*** person ***/
@property (nonatomic,strong) Person *person;
@end

@implementation FriendTableViewController
static NSString * const ID = @"cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    DataSource *dataSource = [[DataSource alloc] init] ;
    self.dataList = [dataSource dataList];
    
    [dataSource savePeopleToIndex];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
- (Person *)findFriendWithId:(NSString *)identifer {
    for (Person *p in self.dataList) {
        if ([p.identifer isEqualToString:identifer]) {
            return p;
        }
    }
    return nil;
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    Person *person = self.dataList[indexPath.row];
    cell.textLabel.text = person.name;
    
    return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Person *person = self.dataList[indexPath.row];
    DXViewController *vc = [[DXViewController alloc] init];
    vc.person = person;
    
    [self.navigationController pushViewController:vc animated:YES];
}

4.2详情界面展示代码:
DXViewController.h

@class Person;
@interface DXViewController : UIViewController
/*** 模型 ***/
@property (nonatomic,strong) Person *person;
@end

DXViewController.m

#import "DXViewController.h"
#import "Person.h"
@interface DXViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@end

@implementation DXViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    Person *person = self.person;
    
    self.imageV.image = [UIImage imageNamed:person.icon];
    self.nameLable.text = person.name;
}

此时尝试点击一项,但是它不会跳转到app的指定区域,只会跳转到对应的app,因为我们还没有指定要跳转的指定区域;

五.跳转到指定界面(详情界面)

在AppDelegate中实现

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
    // 获取你在spotlight中点击的朋友的id
    NSString *friendID = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];
    
    NSLog(@"%@",friendID);
    // 获取到根的导航控制,并且pop掉栈中所有的控制器
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    [navigationController popToRootViewControllerAnimated:NO];
    
    FriendTableViewController *friendVC = (FriendTableViewController *)navigationController.viewControllers.firstObject;
    
    DXViewController *vc = [[DXViewController alloc] init];
    vc.person = [friendVC findFriendWithId:friendID];
    [friendVC showViewController:vc sender:self];
    
    return YES;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,133评论 25 707
  • 作者:AppCoda,原文链接,原文日期:2015-12-22译者:BigbigChai;校对:walkingwa...
    梁杰_numbbbbb阅读 922评论 1 6
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,103评论 4 62
  • 已经不是小学作文里的我和妈妈 一 自从上大学和读研究生,我和妈妈的联系和见面的次数越来越少。在美国读书后工作的最初...
    青姃阅读 139评论 0 0
  • 如梦令—已醉 跌撞层层已醉,摇曳叹叹欲坠。 昨夜恨风清,踌躇无言已对。 已醉,已醉,知否身旁谁睡?
    荞麦皮1阅读 304评论 2 1