iOS新功能, SpotLight应用内搜索

释放双眼,带上耳机,听听看~!

1. 添加所需要的框架


需要两个framework:CoreSpotlightMobileCoreServices,可以直接@import

@import CoreSpotlight;
@import MobileCoreServices;  

2.创建搜索显示的item属性


CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
//设置属性
//1.设置显示内容
NSString *title = [dict objectForKey:@"title"];
//(1)标题
attributeSet.title = title;
//(2)副标题
attributeSet.contentDescription = [NSString stringWithFormat:@"%@, haha", title];
attributeSet.keywords = @[title];
//(3)image
UIImage *image = [UIImage imageNamed:[dict objectForKey:@"image_name"]];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;

3.初始化一个可检索的条目


CSSearchableItem *item;
NSString *identifier = [NSString stringWithFormat:@"%@", attributeSet.title];
/**
展示的控件
* uniqueIdentifier : 唯一的标识符,通过此标识符,可以链接不同设备的同一APP,也是能实现HandOff功能的基础
* domainIdentifier :
* attributeSet : CSSearchableItemAttributeSet 设置item属性(标题, 副标题, image)
- returns: CSSearchableItem
*/
item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:@"com.example.apple_sample.theapp.search" attributeSet:attributeSet];

4.CSSearchableIndex创建, 添加检索入口


[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) {
NSLog(@"Search item indexed");
}];

5.上代码


- (void)spotLightIndexing {
NSString *path =  [[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil];
NSArray  *plistArray = [[NSArray alloc] initWithContentsOfFile:path];
[plistArray enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull dict, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", dict);
CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
//设置属性
//1.设置显示内容
NSString *title = [dict objectForKey:@"title"];
//(1)标题
attributeSet.title = title;
//(2)副标题
attributeSet.contentDescription = [NSString stringWithFormat:@"%@, haha", title];
attributeSet.keywords = @[title];
//(3)image
UIImage *image = [UIImage imageNamed:[dict objectForKey:@"image_name"]];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;
CSSearchableItem *item;
NSString *identifier = [NSString stringWithFormat:@"%@", attributeSet.title];
/**
展示的控件
* uniqueIdentifier :
* domainIdentifier :
* attributeSet : CSSearchableItemAttributeSet 设置item属性(标题, 副标题, image)
- returns: CSSearchableItem
*/
item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:@"com.example.apple_sample.theapp.search" attributeSet:attributeSet];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) {
NSLog(@"Search item indexed");
}];
}];
}

6.删除检索入口


[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:[NSArray arrayWithObjects:@"apple-001", @"apple-002", nil] completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Items deleted");
}
}];
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:[NSArray arrayWithObjects:@"xxx.fruit.com", nil] completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Items deleted");
}
}];
[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"All items deleted");
}
}];

7.截图


这里写图片描述

8.补充:


在iOS9中,新增加了3个类,通过对这三个类的操作与配合,我们可以轻易的在app中添加CoreSpotlight搜索的功能。

CSSearchableItemAttributeSet:设置类,这个类用于设置搜索标签里的icon,内容,图片等。主要用法如下:

//这个类的核心方法只有一个init方法,通过一个类型字符串进行创建,字符串用于在回调中区分
@interface CSSearchableItemAttributeSet : NSObject <NSCopying,NSSecureCoding>
- (instancetype)initWithItemContentType:(nonnull NSString *)itemContentType;
@end
//更多的属性设置在其扩展类中,例如:
@interface CSSearchableItemAttributeSet (CSGeneral)
//展示的名称
@property(nullable, copy) NSString *displayName;
//名称数组
@property(nullable, copy) NSArray<NSString*> *alternateNames;
//完整的路径
@property(nullable, copy) NSString *path;
//链接url
@property(nullable, strong) NSURL *contentURL;
//图片链接的url
@property(nullable, strong) NSURL *thumbnailURL;
//设置图片数据
@property(nullable, copy) NSData *thumbnailData;
//设置一个标识符
@property(nullable, copy) NSString *relatedUniqueIdentifier;
@property(nullable, strong) NSDate *metadataModificationDate;
//内容类型
@property(nullable, copy) NSString *contentType;
@property(nullable, copy) NSArray<NSString*> *contentTypeTree;
//搜索的关键字数组
@property(nullable, copy) NSArray<NSString*> *keywords;
//标题信息
@property(nullable, copy) NSString *title;
@end

CSSearchableItem:搜索标签类,通过这个类,来创建响应的搜索标签。主要内容如下:

//这个类主要用于创建搜索的标签
@interface CSSearchableItem : NSObject <NSSecureCoding, NSCopying>
//init方法
- (instancetype)initWithUniqueIdentifier:(nullable NSString *)uniqueIdentifier //Can be null, one will be generated
domainIdentifier:(nullable NSString *)domainIdentifier
attributeSet:(CSSearchableItemAttributeSet *)attributeSet;
//相应 的属性
@property (copy) NSString *uniqueIdentifier;
@property (copy, nullable) NSString *domainIdentifier;
@property (copy, null_resettable) NSDate * expirationDate;
@property (strong) CSSearchableItemAttributeSet *attributeSet;
@end

CSSearchableIndex:通过它对标签进行增、删、改、查等操作:

@interface CSSearchableIndex : NSObject
@property (weak,nullable) id<CSSearchableIndexDelegate> indexDelegate;
//判断设备是否支持
+ (BOOL)isIndexingAvailable;
//取系统的searchIndex管理者
+ (instancetype)defaultSearchableIndex;
//一般情况下,我们不需要重新创建对象
- (instancetype)initWithName:(NSString *)name;
- (instancetype)initWithName:(NSString *)name protectionClass:(nullable NSString *)protectionClass;
//设置索引标签
- (void)indexSearchableItems:(NSArray<CSSearchableItem *> *)items completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;
//删除指定id索引标签
- (void)deleteSearchableItemsWithIdentifiers:(NSArray<NSString *> *)identifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;
- (void)deleteSearchableItemsWithDomainIdentifiers:(NSArray<NSString *> *)domainIdentifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;
//删除所有索引标签
- (void)deleteAllSearchableItemsWithCompletionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;
@end

人已赞赏
iOS文章

iOS音频播放 (二):AudioSession

2020-3-18 11:24:10

iOS文章

iOS 进阶-高阶_多线程_Pthread

2020-3-18 11:44:03

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索