Spotlight has made some new improvements on iOS9, which is to open up some new APIs, and with the Core Spotlight Framework You can integrate Spotlight into your app. The integrated Spotlight app can search the content of the app in spotlight and open the relevant page via content. Because of the development task, the boss said to let in the app support Spotlight, and then engaged in the official Apple documents. It can be said that the integration of spotlight is not complicated, the official online speaking also quite clear, today's blog through a demo to integrate spotlight.
Apple's official links to the core Spotlight framework are as follows:
Https://developer.apple.com/library/prerelease/ios/documentation/CoreSpotlight/Reference/CoreSpotlight_ framework/index.html#//apple_ref/doc/uid/tp40016250
One. Demo Run effect
or through a demo to introduce, demo running effect is as follows. We have content about Hayao Miyazaki in our app, and then search for Hayao Miyazaki in spotlight to search for relevant content, and you can click to open to show relevant content. The specific operating results are as follows:
Two. Integrated Core Spotlight Framework
1. To use Spotlight in the app, first introduce the core Spotlight Framework,Targets->general, linked frameworks and Libraries Click the plus sign to add corespotlight.framework. as shown below.
2. Introduce the <CoreSpotlight/CoreSpotlight.h> header file into the corresponding view controller and then start writing code to enable your app content to support Spotlight search. Here is the code for adding spotlight to the demo. Spotlight search for things, each item is an entry that is Cssearchableitem object, and the object is associated with a collection of properties (Cssearchableitemattributeset ) The collection stores the Cssearchableitem related properties of the object, if title, ContentDescription (content Introduction),
Thumbnaildata (picture) and other required content. For details, see the code description and code comments below.
Code Description:
(1). A temp array is defined first to store the keyword searched in Spotlight, which is the app content that spotlight can search for. The contents of the array are iterated through a series of steps to correlate the spotlight.
(2) Each time a content array is traversed, a cssearchableitemattributeset (property collection) is created and values are assigned to some properties in the property collection. You then create a cssearchableitem that associates the corresponding set of properties when you create the Cssearchableitem. Place each created entry into a mutable array, since all entries are created and associated with the Spotlight index (CSSEARCHABLEINDEX).
(3) Get the Cssearchableindex object through a singleton and associate it with the Cssearchableitem array we created. The specific code and steps are as follows.
1- (void) Supportspotlightsearch {2dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);3Dispatch_async (Queue, ^{4 @try {5Nsarray *temp = @[@"Hayao Miyazaki-Dragon Cat",@"Hayao Miyazaki-Thousand and Chihiro",@"Hayao Miyazaki-City of the skies"];6 7 //creating an array of Searchableitems8Nsmutablearray *searchableitems =[[Nsmutablearray alloc] initWithCapacity:temp.count];9 Ten for(inti =0; i < Temp.count; i + +) { One A //1. Create an attribute collection for an entry -Cssearchableitemattributeset * AttributeSet = [[Cssearchableitemattributeset alloc] Initwithitemcontenttype: ( nsstring*) Kuttypeimage]; - the //2. Adding attributes to a collection of properties -Attributeset.title =Temp[i]; -Attributeset.contentdescription = [NSString stringWithFormat:@"Hayao Miyazaki and%@", Temp[i]]; -Attributeset.thumbnaildata = Uiimagepngrepresentation ([UIImage imagenamed:[nsstring stringWithFormat:@"%d.png", i+1]]); + - //3. Attribute collections are associated with entries +Cssearchableitem *searchableitem = [[Cssearchableitem alloc] initwithuniqueidentifier:[nsstring StringWithFormat:@"%d", i+1] Domainidentifier:@"Zeluli.spotlightsearchdemo"Attributeset:attributeset]; A at //Staging the entry - [Searchableitems Addobject:searchableitem]; - } - - //4. The entry array is associated with the index -[[Cssearchableindex Defaultsearchableindex] Indexsearchableitems:searchableitems completionHandler:^ (NSError *_nullable Error) { in if(!error) { -NSLog (@"%s,%@", __function__, [Error localizeddescription]); to } + }]; - } the @catch(NSException *exception) { *NSLog (@"%s,%@", __function__, exception); $ }Panax Notoginseng @finally { - the } + }); A}
3. Handle the event that the entry is clicked on after the search, and the handling of the event is handled in the following delegate agent method in Appdelegate. The following idetifier is the unique identifier specified when the property collection is associated with an entry.
1-(BOOL) Application: (nonnull uiapplication *) application continueuseractivity: (nonnull nsuseractivity *) userActivity Restorationhandler: (nonnullvoid(^) (Nsarray *__nullable)) restorationhandler{2 3NSString *idetifier = useractivity.userinfo[@"Kcssearchableitemactivityidentifier"];4 5Uinavigationcontroller *navigationcontroller = (Uinavigationcontroller *) Self.window.rootViewController;6 7Viewcontroller *VC = [Navigationcontroller viewcontrollers][0];8[Vc.myimage setimage:[uiimage imagenamed:[nsstring stringWithFormat:@"%@.png", Idetifier]];9 Ten One returnYES; A}
Demo Share address--github:https://github.com/lizelu/SpotlightSearchDemo
Content of the core Spotlight framework search app in iOS development integration iOS9