Home>

As one of the important features of Apple iOS9,Spotlight search is now back to the leftmost side of the main interface (also supports the main interface to slide out). With API support, it also brings a new universal search universal search functionIn addition to the network and the system itself,You can also search directly for related content in third-party apps.Let's take a closer look at spotlight

1.spotloight what?

Lightspotlight has made some new improvements on ios9, That is, some new APIs are open. With the core spotlight framework, you can integrate spotlight in your app. Spotlight-integrated apps can search for app content in spotlight and open related pages through the content.

Demo demo

2. How to integrate spotlight

A. Add the required framework

#if __iphone_os_version_max_allowed>= 90000
#import<corespotlight/corespotlight.h>
#import<mobilecoreservices/mobilecoreservices.h>
#endif

Note that many apps support below ios9, so adding #if __iphone_os_version_max_allowed>= 90000 can solve the problem of device crashes under ios9

B. Create cssearchableitemattributeset object

</p>
<p>
cssearchableitemattributeset * attributeset=[[cssearchableitemattributeset alloc] initwithitemcontenttype:(nsstring *) kuttypeimage];
 attributeset.title=spotlighttitle;//title
 attributeset.keywords=keywords;//keywords, nsarray format
 attributeset.contentdescription=spotlightdesc;//description
 attributeset.thumbnaildata=photo;//icon, nsdata format
</p>
<p>
//Method to convert pictures into nsdata
Uiimagepngrepresentation ([uiimage imagenamed:@"xxx.png"]
</p>
<p>

C. Create searchable item cssearchableitem

//spotlightinfo can be passed to somewhere as some data
//domainid id, use this id to determine which spotlight it is
cssearchableitem * item=[[cssearchableitem alloc] initwithuniqueidentifier:spotlightinfo domainidentifier:domainid attributeset:attributeset];

D. Add search entry

[[cssearchableindex defaultsearchableindex] indexsearchableitems:@[item] completionhandler:^ (nserror * error) {
 if (error) {
 nslog (@ "indexsearchableitems error:%@", error.localizeddescription);
 }
}];

========= Complete code ========

-(void) insertsearchableitem:(nsdata *) photo spotlighttitle:(nsstring *) spotlighttitle description:(nsstring *) spotlightdesc keywords:(nsarray *) keywords spotlightinfo:(nsstring *) spotlightinfo domainid:(nsstring *) domainid {
 cssearchableitemattributeset * attributeset=[[cssearchableitemattributeset alloc] initwithitemcontenttype:(nsstring *) kuttypeimage];
 attributeset.title=spotlighttitle;//title
 attributeset.keywords=keywords;//keywords, nsarray format
 attributeset.contentdescription=spotlightdesc;//description
 attributeset.thumbnaildata=photo;//icon, nsdata format
 //spotlightinfo can be passed to somewhere as some data
 //domainid id, use this id to determine which spotlight it is
 cssearchableitem * item=[[cssearchableitem alloc] initwithuniqueidentifier:spotlightinfo domainidentifier:domainid attributeset:attributeset];
 [[cssearchableindex defaultsearchableindex] indexsearchableitems:@[item] completionhandler:^ (nserror * error) {
 if (error) {
 nslog (@ "indexsearchableitems error:%@", error.localizeddescription);
 }
 }];
}

========= How to load local pictures =========

[self insertsearchableitem:uiimagepngrepresentation ([uiimage imagenamed:@"xxx.png"]) spotlighttitle:@"Wait for the wind to come" description:@"Wait for the wind to come描述" keywords:@[@ "Abalone whale",@"Dahlia" ] spotlightinfo:@"Pass past values" domainid:@"com.wb.spotlight"];

========= Load the use of network pictures =========

dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^ {
 nsdata * data=[nsdata datawithcontentsofurl:[nsurl urlwithstring:@""]];
 [self insertsearchableitem:data spotlighttitle:@"Wait for the wind to come" description:@"Wait for the wind to come描述" keywords:@[@ "Abalone whale",@"Dahlia"] spotlightinfo:@"Pass past values" domainid:@"com.wb.spotlight"];
 });

========= Method to remove all spotlights =========

[[cssearchableindex defaultsearchableindex] deleteallsearchableitemswithcompletionhandler:^ (nserror * _nullable error) {
if (error) {
nslog (@ "%@", error.localizeddescription);
}
}];

========= Method to delete the specified spotlight =========

[[cssearchableindex defaultsearchableindex] deletesearchableitemswithdomainidentifiers:@"domainid" completionhandler:^ (nserror * _nullable error) {
if (error) {
nslog (@ "%@", error.localizeddescription);
}
}];

========= Response method after clicking spotlight =========

-(bool) application:(uiapplication *) application continueuseractivity:(nsuseractivity *) useractivity restorationhandler:(void (^) (nsarray * _nullable)) restorationhandler {
if ([[useractivity activitytype] isequaltostring:cssearchableitemactiontype]) {
nsstring * uniqueidentifier=[useractivity.userinfo objectforkey:cssearchableitemactivityidentifier];
//accept pre-defined values,If there are multiple parameters, you can use json to string to pass it.
After accepting, convert the string to json
nslog (@ "Passed value%@", uniqueidentifier);
}
return yes;
}

Note:

#if __iphone_os_version_max_allowed>= 90000
//Related spotlight methods, etc.
#endif
//spotlight supports running on ios9 and above devices,Need to add this to prevent crash issues with older devices
ios
  • Previous Python implements mysql single quoted string filtering method
  • Next Judgment of parameter structure of empty and isset in PHP and the difference between empty () and isset ()