【IOS工具类】IOS9的CoreSpotlight(OC语言)
2015-08-12 11:38
351 查看
什么是CoreSpotlight?就是在IOS9下,让用户在下拉的搜索页面里可以搜索到你的应用。
PS:
上传framework:
Swift版本:http://www.csdn.net/article/2015-07-16/2825222-search-apis
#define IS_IOS_9 ([[[UIDevice currentDevice] systemVersion] floatValue] >=9.0 ? YES : NO)
split就是调用NSString的 componentsSeparatedByString 方法;
NSString+Wapper.h 类参照:http://blog.csdn.net/coooliang/article/details/45871149
#import <Foundation/Foundation.h> @interface IOS9SearchAPIUtil : NSObject +(IOS9SearchAPIUtil *)sharedInstance; -(void)addSearchItemsArray:(NSArray *)array; @end
#import "IOS9SearchAPIUtil.h" #import <CoreSpotlight/CoreSpotlight.h> #import <MobileCoreServices/MobileCoreServices.h> #import "NSString+Wrapper.h" #import <UIKit/UIKit.h> #define IOS9SearchAPIUtil_domainIdentifier @"com.searchapi.shows" @implementation IOS9SearchAPIUtil +(IOS9SearchAPIUtil *)sharedInstance{ static IOS9SearchAPIUtil *instance = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ instance = [[self alloc] init]; }); return instance; } -(void)addSearchItemsArray:(NSArray *)array{ if(IS_IOS_9){ NSMutableArray <CSSearchableItem *> *searchableItems = [NSMutableArray arrayWithCapacity:0]; for (NSDictionary *d in array) { NSString *title = [d objectForKey:@"title"]; NSString *desc = [d objectForKey:@"desc"]; NSString *time = [d objectForKey:@"time"]; NSString *nid = [d objectForKey:@"nid"]; CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:@"SearchAPIViews"]; attributeSet.title = title; attributeSet.contentDescription = [NSString stringWithFormat:@"%@\n%@",desc,time]; NSMutableArray *keywords = [NSMutableArray arrayWithArray:[title split:@" "]]; [keywords addObject:desc]; attributeSet.keywords = keywords; NSString *identifiner = [NSString stringWithFormat:@"%@",nid]; [searchableItems addObject:[[CSSearchableItem alloc]initWithUniqueIdentifier:identifiner domainIdentifier:IOS9SearchAPIUtil_domainIdentifier attributeSet:attributeSet]]; } [[CSSearchableIndex defaultSearchableIndex]indexSearchableItems:searchableItems completionHandler:^(NSError * __nullable error) { if(error != nil){ NSLog(@"%@",error.localizedDescription); }else { NSLog(@"Items were indexed successfully"); } }]; } } @end使用方法:
[[IOS9SearchAPIUtil sharedInstance]addSearchItemsArray:@[@{@"nid":@"fin://1238796",@"title":@"fin",@"desc":@"基金",@"time":@"2014-01-01"},@{@"nid":@"fun://54fdsaf32",@"title":@"fun",@"desc":@"理财",@"time":@"2016-01-01"}]];效果:
PS:
上传framework:
Swift版本:http://www.csdn.net/article/2015-07-16/2825222-search-apis
#define IS_IOS_9 ([[[UIDevice currentDevice] systemVersion] floatValue] >=9.0 ? YES : NO)
split就是调用NSString的 componentsSeparatedByString 方法;
NSString+Wapper.h 类参照:http://blog.csdn.net/coooliang/article/details/45871149
相关文章推荐
- 智能指针学习
- Bit Manipulation - Bitwise AND of Numbers Range
- 深入分析C++引用
- C++中const用法总结
- C++中const修饰二级指针(从类型‘int**’到类型‘const int**’的转换无效)
- C++11中的右值引用
- C++_const成员函数
- N的阶乘(N!)中的末尾有多少个0?
- 第七篇:C/C++动态内存的分配问题
- C++调试时的错误例子及解决方法
- IP的int型与char*型相互转化
- [leetcode-120]Triangle(c++)
- 终端编译C语言程序不变色,要配置vim
- C++实现线程池
- Bit Manipulation - Subsets
- C语言中的malloc使用详解
- c++ 编程 几个有用的宏
- C++ Primer快速入门之七:运算符
- C++ Socket C/S ,实现客户端,服务器端断开重连
- C语言实现将数字颠倒