iOS9中spotlight的简单使用
2015-10-28 10:26
465 查看
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
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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音
- 解析iOS开发中的FirstResponder第一响应对象