IOS 9 Spotlight search OC edition, spotlightoc

Source: Internet
Author: User

IOS 9 Spotlight search OC edition, spotlightoc

Introduction:
At the WWDC 2015 Conference, Apple officially announced iOS9. In addition to many new features and enhancements, this upgrade also gives developers a chance to discover and use the content in their apps through the Spotlight search function. The new APIs available in iOS9 allows you to index the content or interface status in the APP and use Spotlight for users. The three major components of these new search APIs are:

* NSUserActivity class, which is designed to be visible to the APP content
* Core Spotlight framework: zookeeper designed for any APP content
* Web markup is designed for this type of APP, that is, the APP content has an image repository on a website.


In this tutorial, I will show you how to use the NSUserActivity class and Core Spotlight framework in your application.

Preparations:
This tutorial requires you to run on Xcode7 and OSX 10.10, iOS9.0, or a later system.

Procedure:

#import <CoreSpotlight/CoreSpotlight.h>

2. Create a search property object

 CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@""];

3. Set search attributes

// Search for the displayed name attributeSet. title = obj. name; // display description attributeSet. contentDescription = obj. desc; // search for the keyword attributeSet. keywords = @ [obj. name, @ "CX"]; // The displayed icon UIImage * icon = [UIImage imageNamed: obj. imageName]; if (icon) {attributeSet. thumbnailData = UIImageJPEGRepresentation (icon, 1 );}

 
4. Create a search object based on the search attributes (domainIdentifier: Unique identifier)

CSSearchableItem * item = [[CSSearchableItem alloc] initWithUniqueIdentifier:obj.name domainIdentifier:SearchDomain attributeSet:attributeSet];

5. Add the search object to the search Array

[searchItems addObject:item];

6. Set the INDEX DIRECTORY

CSSearchableIndex * searchableIndex = [CSSearchableIndex defaultSearchableIndex]; [searchableIndex indexSearchableItems: searchItems completionHandler: ^ (NSError * _ Nullable error) {if (error! = Nil) {// NSLog (@ "% @", [error localizedDescription]) failed to add the index;} else {// successful NSLog (@ "indexing successful") ;}}];

 
7. Implement the AppDelegate method (you can use spotlight to search for the content in the APP and click the content to enter the APP. This method will be called)

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{            UINavigationController * vc = (UINavigationController *)self.window.rootViewController;                    [vc.topViewController restoreUserActivityState:userActivity];                    return YES;        }

 


8. Implement the search list controller (in the activity, you can click all the attributes of a piece of data in the spotlight search list to perform corresponding operations based on the attributes)

 - (void)restoreUserActivityState:(NSUserActivity *)activity{}

 

Code address

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.