What is Corespotlight? Just under IOS9, let the user search for your app in the dropdown search page.
#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 *> *searchable Items = [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:I Os9searchapiutil_domainidentifier Attributeset:attributeset]]; } [[Cssearchableindex Defaultsearchableindex]indexsearchableitems:searchableitems completionHandler:^ (NSErr or * __nullable error) {if (Error! = nil) {NSLog (@ "%@", error.localizeddescription); }else {NSLog (@ "Items were indexed successfully"); } }]; }} @endHow to use:
[[Ios9searchapiutil sharedinstance]addsearchitemsarray:@[@{@ "nid": @ "fin://1238796", @ "title": @ "Fin", @ "desc": @ "fund @ "Time": @ "2014-01-01"},@{@ "nid": @ "Fun://54fdsaf32", @ "title": @ "Fun" @ "desc": @ "Money" @ "time": @ "2016-01-01"}];
Effect:
Ps:
Upload framework:
Swift version: Http://www.csdn.net/article/2015-07-16/2825222-search-apis
#define IS_IOS_9 ([[[Uidevice Currentdevice] systemversion] floatvalue] >=9.0 ? Yes:no)
Split is the Componentsseparatedbystring method of calling NSString;
Nsstring+wapper.h class Reference: http://blog.csdn.net/coooliang/article/details/45871149
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"iOS Tools Class" IOS9 corespotlight (OC language)