IOS9-CoreSpotlight到底怎么用?(OC语言版)
来源:互联网 发布:sqlserver count 编辑:程序博客网 时间:2024/04/29 04:36
什么是CoreSpotlight?简单的说:就是在IOS9下,让用户在下拉的搜索页面里可以搜索到你的应用。
#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 <UIKit/UIKit.h>BOOL IS_IOS_9 = NO;#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 ([[UIDevice currentDevice].systemVersion floatValue]>=9.0) { IS_IOS_9 = YES; } 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"]; NSLog(@"d----%@",d); CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:@"SearchAPIViews"]; attributeSet.title = title; attributeSet.contentDescription = [NSString stringWithFormat:@"%@\n%@",desc,time];// NSMutableArray *keywords = [NSMutableArray arrayWithArray:[title split:@" "]]; NSMutableArray *keywords = [NSMutableArray arrayWithArray:[title componentsSeparatedByString:@" "]]; [keywords addObject:desc]; attributeSet.keywords = keywords; NSString *identifiner = [NSString stringWithFormat:@"%@",nid]; //应该是唯一的,以你的应用程序组。 // REQUIRED因为这是将参考项的方式来更新索引/从索引中删除 //首先是一个UUID,易于使用的,但你可以用自己的UID更换之前,如果你希望该项目第一个索引。 [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
#import "AppDelegate.h"#import "IOS9SearchAPIUtil.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //添加数据源 [[IOS9SearchAPIUtil sharedInstance]addSearchItemsArray:@[@{@"nid":@"fin://1238796",@"title":@"fin",@"desc":@"基金",@"time":@"2014-01-01"},@{@"nid":@"fun://54fdsaf32",@"title":@"fun",@"desc":@"理财",@"time":@"2016-01-01"}]]; return YES;}
1 0
- IOS9-CoreSpotlight到底怎么用?(OC语言版)
- ios9 CoreSpotlight
- 【IOS工具类】IOS9的CoreSpotlight(OC语言)
- iOS开发--iOS9 SearchApi CoreSpotlight的使用
- iOS9搜索三剑客之CoreSpotlight
- iOS 8 NSUserActivity和iOS9 CoreSpotlight 搜索API的使用
- urlrewriter到底怎么用
- onclicklistener到底怎么用?
- onclicklistener到底怎么用?
- onclicklistener到底怎么用?
- OnClicklistener到底怎么用?
- 数据结构(C语言版)要怎么学
- didReceiveMemoryWarning到底应该怎么用
- cocos2dx FadeIn到底怎么用?
- glVertexAttribPointer 到底该怎么用?
- 谁能告诉我数据结构(C语言版)怎么学啊
- [全程建模]UML到底该怎么用
- android中的Context到底该怎么用
- PHP的输出缓冲区
- struts升级到2.3出现各种问题
- Attribute 和 Parameter 的区别
- jfinalQ开发教程01-表命名规范和代码生成
- request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath
- IOS9-CoreSpotlight到底怎么用?(OC语言版)
- 进程&线程(持续更)
- Matlab小程序-正弦波线性组合频谱图
- 产品思维------三维思考模型
- jfinalQ开发教程02-权限系统和一对多的处理
- neutron(1)veth pair
- jfinalQ开发教程03-加密部署
- iOS开发之日期
- 二、Python课程【初级篇】