您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS Spotlight