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