IOS 9 Spotlight Search OC

Source: Internet
Author: User

Introduction :
At the WWDC 2015 meeting, Apple officially announced the iOS9. Apart from many new features and enhancements, this upgrade also gives developers a chance to find and use the content of their app through the spotlight search function. The new APIs available in iOS9 allow you to index the contents of the app or the state of the interface, using Spotlight for the user. The three main components of these new search APIs are:

* Nsuseractivity class, which is designed for visible app content??
* Core Spotlight Framework, designed for any app content??
* Web markup, designed for this type of app, is the app's content mirrored on a website?


In this tutorial, I'll show you how you can use the Nsuseractivity class and the Core Spotlight framework in your app.

preparatory work :
This tutorial requires you to run the system in Xcode7 and OSX 10.10, iOS9.0 system or later

Steps :

#import <CoreSpotlight/CoreSpotlight.h>

2. Create a Search Property object

Cssearchableitemattributeset * AttributeSet = [[Cssearchableitemattributeset alloc] initwithitemcontenttype:@ ""];

3. Set Search Properties

Search Displays the name            attributeset.title = obj.name;            Description of the display            attributeset.contentdescription = Obj.desc;            Search keyword            attributeset.keywords = @[obj.name,@ "CX"];            Icon displayed            UIImage * icon = [UIImage imageNamed:obj.imageName];            if (icon) {                    Attributeset.thumbnaildata = uiimagejpegrepresentation (icon, 1);            }


4. Create search objects based on search properties (domainidentifier: Unique identity)

Cssearchableitem * item = [[Cssearchableitem alloc] InitWithUniqueIdentifier:obj.name domainidentifier:searchdomain Attributeset:attributeset];

5. Adding a 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) {//Add index failed            NSLog (@ "%@", [Error localizeddescription]);        } else{//Success            NSLog (@ "Indexing successful");        }    ];


7. Implement the Appdelegate method (users can call this method when they click through the content of Spotlight search into the app)

-(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. In the search list controller implementation method (activity in which the user clicks on the Spotlight search list of a piece of data of all attributes to do the corresponding action)

-(void) Restoreuseractivitystate: (Nsuseractivity *) activity{}

Code address

IOS 9 Spotlight Search OC

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.