Integrate iOS development with Core Spotlight Framework in iOS9 to search for App content, ios9spotlight

Source: Internet
Author: User

Integrate iOS development with Core Spotlight Framework in iOS9 to search for App content, ios9spotlight

Spotlight has made some new improvements on iOS9, that is, opening up some new APIs. With the Core Spotlight Framework, You can integrate Spotlight in your app. Apps integrated with Spotlight can search for App content in Spotlight and open relevant pages through the content. After receiving a development task, the boss said that he had supported Spotlight in the App, so he started Apple's official documentation. It can be said that the integration of Spotlight is not complicated, and it is quite clear on the official website. Today, the blog will integrate Spotlight through a Demo.

Apple's official link to the Core Spotlight Framework is as follows:

Https://developer.apple.com/library/prerelease/ios/documentation/CoreSpotlight/Reference/CoreSpotlight_Framework/index.html#//apple_ref/doc/uid/TP40016250

I. Demo Running Effect

You can also use a Demo to introduce the running effect of the Demo as follows. Our App contains content about Hayao Miyazaki. Then, you can search for Hayao Miyazaki in Spotlight to find the relevant content and click to open the relevant content. The specific running effect is as follows:

2. integrate Core Spotlight Framework

1. To use Spotlight in an App, you must first introduce the Core Spotlight Framework, Targets-> General-> linked Frameworks and Libraries, and click the plus sign to add CoreSpotlight. framework. As shown below.

2. Introduce the header file <CoreSpotlight/CoreSpotlight. h> in the corresponding View Controller, and then write code to enable your App content to support Spotlight search. The following code adds Spotlight to the Demo. Each item searched by Spotlight is an object of CSSearchableItem, and the modified object is associated with a set of attributes (CSSearchableItemAttributeSet). The CSSearchableItem object attributes are stored in the set, if title, contentDescription ),

ThumbnailData (image) and other required content. For details, see the code description and code comments below.

Code Description:

(1) First, a temp array is defined to store the keywords searched in Spotlight, that is, the App content that can be searched by Spotlight. The content in the array is traversed cyclically and connected to Spotlight through a series of steps.

(2) Each time you traverse the content array, you need to create a CSSearchableItemAttributeSet (Attribute Set) and assign values to some attributes in the attribute set. Then create a CSSearchableItem. When creating a CSSearchableItem, associate its corresponding property set. Save each created entry to a variable array, because after all the entries are created, they must be associated with the Spotlight index (CSSearchableIndex.

(3) obtain the CSSearchableIndex object through a single example and associate it with the created CSSearchableItem array. The code and steps are as follows.

1-(void) supportSpotlightSearch {2 dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 3 dispatch_async (queue, ^ {4 @ try {5 NSArray * temp = @ [@ "Hayao Miyazaki-longmao", @ "Hayao Miyazaki-Qianyi qianxun", @ "Hayao Miyazaki-city in the sky"]; 6 7 // create the SearchableItems array 8 NSMutableArray * searchableItems = [[NSMutableArray alloc] initWithCapacity: temp. count]; 9 10 for (int I = 0; I <temp. count; I ++) {11 12 // 1. create a set of attributes 13 CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType :( NSString *) kUTTypeImage]; 14 15 // 2. add attribute 16 attributeSet to the attribute set. title = temp [I]; 17 attributeSet. contentDescription = [NSString stringWithFormat: @ "Hayao Miyazaki and % @", temp [I]; 18 attributeSet. thumbnailData = UIImagePNGRepresentation ([UIImage imageNamed: [NSString stringWithFormat: @ "mongod.png", I + 1]); 19 20 // 3. 21 CSSearchableItem * searchableItem = [[CSSearchableItem alloc] initWithUniqueIdentifier: [NSString stringWithFormat: @ "% d", I + 1] domainIdentifier: @ "ZeluLi. spotlightSearchDemo "attributeSet: attributeSet]; 22 23 // Save the entry 24 [searchableItems addObject: searchableItem]; 25} 26 27 // 4. bar entry array and index Association 28 [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems: searchableItems completionHan Dler: ^ (NSError * _ Nullable error) {29 if (! Error) {30 NSLog (@ "% s, % @", _ FUNCTION __, [error localizedDescription]); 31} 32}]; 33} 34 @ catch (NSException * exception) {35 NSLog (@ "% s, % @", _ FUNCTION __, exception ); 36} 37 @ finally {38 39} 40}); 41}

 

3. process the click event after the search. The event is processed in the delegate proxy method in AppDelegate. The following idetifier is the unique identifier specified when the attribute set is associated with the 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     10     11     return YES;12 }

 

DEMO share address -- github: https://github.com/lizelu/SpotlightSearchDemo

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.