iOS9中spotlight的简单使用

1.使用环境

Xcode 7.0 +

iOS9.0 +


2.使用目的

iOS9之前在搜索中只能搜索到应用名称,使用iOS9提供的新API可以建立自己应用中的索引,引导用户更好地使用


3.具体代码和注释

1)首先需要在 targets ---> general --> Linked Frameworks and Libraries 中导入 CoreSpotlight.frame 和 MobileCoreService.framework

2)

#import "ViewController.h"

//导入对应头文件

#import <CoreSpotlight/CoreSpotlight.h>

#import <MobileCoreServices/MobileCoreServices.h>

// 定义搜索ID

#define SEARCH_ID @"com.yiche.SpotLightDemo.search"


@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

// 判断系统版本,大于9.0才可以使用

    if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {

        [self saveData];

    }

}


- (void)saveData {

// 先根据ID删除索引,则可以在每次调用这个方法的时候实现更新的效果。同时节省存储空间

    [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@[SEARCH_ID] completionHandler:^(NSError * _Nullable error) {

    }];

// 加载数据(也可以在外部加载成功之后传入当前方法)

    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

    NSArray *plistArray = [[NSArray alloc] initWithContentsOfFile:path];

// 遍历数组中的数据。因为数组中元素的类型是NSDictionary,所以对应到block中的第一个参数就由id改为NSDictionary

    [plistArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull dict, NSUInteger idx, BOOL * _Nonnull stop) {

// 创建attributeSet,设置类型,类型的具体含义表示搜索中显示图片的类型

        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];

        

        // 设置每条搜索出来的信息前面显示的图片

       NSString *imageName = [dict objectForKey:@"image_name"];

UIImage *image = [UIImage imageNamed:imageName];

      NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];

      attributeSet.thumbnailData = imageData;

// 设置标题

NSString *title = dict[@"title"];

       attributeSet.title = title;

// 设置内容描述

       attributeSet.contentDescription = [NSString stringWithFormat:@"这是描述文字 %@",imageName];

// 设置关键字(如果设置了关键字,则在搜索关键字、标题、内容的时候都会显示)

       attributeSet.keywords = @[@"漂亮"];

      

// 设置搜索item

       CSSearchableItem *item;

// 这个标志需要设置成唯一的

       NSString *identifier = [NSString stringWithFormat:@"%@",attributeSet.title];

       item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:SEARCH_ID attributeSet:attributeSet];

        

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

           NSLog(@"something you want to see ");

       }];

   }];

    

}

@end


4.搜索到信息之后,点击信息可以回到app,会调用AppDelegate中下面的方法:

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

    NSLog(@"点击了搜索到的数据");

NSString *identifier = userActivity.userInfo[CSSearchableItemActivityIdentifier];

    NSLog(@"搜索标识是:%@",identifier);


//可以根据identifier做页面的跳转


    return YES;

}


5.注意
搜索时默认显示3条数据,可以点击右侧“展开”可以查看更多



参考:http://blog.csdn.net/mengxiangyue/article/details/46575977

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值