Content of the core Spotlight framework search app in iOS development integration iOS9

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/ludashi/p/4867511.html

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 attributes (Cssearchableitemattributeset This collection stores the related properties of the Cssearchableitem 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).

The

(3) obtains the Cssearchableindex object through a singleton and associates it with the Cssearchableitem array that we created. The specific code and steps are as follows.

 1-(void) Supportspotlightsearch {2 dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_defau LT, 0);              3 Dispatch_async (Queue, ^{4 @try {5 Nsarray *temp = @[@ "Hayao Miyazaki-Dragon Cat" @ Miyazaki-Thousand and Chihiro, @ Miyazaki-Sky City]; 6 7//Create an array of Searchableitems 8 nsmutablearray *searchableitems = [[Nsmutablearray alloc] In ItWithCapacity:temp.count]; 9 for (int i = 0; i < temp.count; i + +) {11 12//1. Create an attribute collection of entries Cssearchableitemattributeset * AttributeSet = [[Cssearchableitemattributeset alloc] Initwithitemcontent Type: (nsstring*) kuttypeimage];14 15//2. Add a property to a property collection. Attributeset.title =                 temp[i];17 attributeset.contentdescription = [NSString stringwithformat:@ "Hayao Miyazaki and%@", temp[i]];18 Attributeset.thumbnaildata = Uiimagepngrepresentation ([UIImage imagenamed:[nsstring stringwithformat:@ "%d. png ", i+1]]); 19 20//3. Attribute collection is associated with an entry cssearchableitem *searchableitem = [ [Cssearchableitem alloc] initwithuniqueidentifier:[nsstring stringwithformat:@ "%d", i+1] domainidentifier:@ "                 Zeluli.spotlightsearchdemo "attributeset:attributeset];22 23//Hold the entry for staging 24 [Searchableitems addobject:searchableitem];25}26 27//4. The entry array is associated with the index 28 [[ Cssearchableindex Defaultsearchableindex] Indexsearchableitems:searchableitems completionHandler:^ (NSError * _ Nullable error) {if (!error) {NSLog (@ "%s,%@", __function__, [Error Localizeddes Cription]);}32}];33}34 @catch (nsexception *exception) {NS Log (@ "%s,%@", __function__, Exception),}37 @finally {38 39}40}); 41}

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: (nonnull void (^) (Nsarray * __nullable)) restorationhandler{2      3     NSString * Idetifier = useractivity.userinfo[@ "Kcssearchableitemactivityidentifier"]; 4      5     uinavigationcontroller *navigationcontroller = (Uinavigationcontroller *) Self.window.rootViewController; 6      7     viewcontroller *VC = [Navigationcontroller viewcontrollers][0]; 8     [Vc.myimage setimage:[uiimage Imagenamed:[nsstring stringwithformat:@ "%@.png", Idetifier]]; 9     return yes;12}

Demo Share Address--github:https://github.com/lizelu/spotlightsearchdemo

Content of the core Spotlight framework search app in iOS development integration iOS9

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.