IOS 9 in-app search (corespotlight) API

Source: Internet
Author: User
Tags new set to domain

Gold field (GitHub sample source code)

Preface

Before iOS9 we can only use Spotlight to search the app name to open the specified app, while the rest of the content is provided to the system (information, contacts, mail, etc.). After IOS9, Apple allows developers to set up any content in the app that can be indexed by spotlight and what happens when a user selects a search content. As shown in the demo:

Figure 1 Spotlight search in-app results

Simple Introduction

There are three APIs available in iOS9 to help us implement the search, as follows:

Figure 2 IOS 9 new Search API

nsuseractivity

Nsuseractivity contains new methods and properties to help us implement index activities and application state to make them available in search results. Each application can use the Nsuseractivity API to generate content that is more useful to the user. Incidentally, the handoff of Nsuseractivity in IOS8 has been introduced.

Web Markup

This feature allows the app to mirror its own content and establish its own references in spotlight. Apple crawlers crawl the markup content on your site, which is available to safari and spotlight. The magic of this feature is that. The user does not need to install your app on the phone. This way your app can show more to potential users. Apple's cloud service will index your content and make it a lot more rewarding to keep your app in depth with the public Search API.

Core Spotlight

The new Corespotlight (framework) is a new set of APIs provided by IOS9 to help you build an index in your app. Corespotlight is used to process user data such as documents, photos, and other types of user-generated content.

let's get started.preparatory work

First we use three different viewcontroller to load different user data:

    • Image
    • Txt
    • Web

And then use a TableView to index their part of the code without repeating the specifics of the structure

Figure 3 Demo Initialization

Figure 4 IPhone Demo

All right, everything is corespotlight.

Corespotlight

Import the framework First
Project->buildphases->link Binary with libraries-> search Corespotlight
If the search is not available, make sure you are using Xcode7

Figure 5 Adding the Corespotlight frame

And then there's the most important part of the code.

First, import the header file

Then save the data that needs to be indexed to Corespotlight

-(void) savedata{
Nsmutablearray *seachableitems = [Nsmutablearray new];
[Self.tabledata enumerateobjectsusingblock:^ (nsstring *__nonnull obj, Nsuinteger idx, BOOL * __nonnull stop) {
cssearchableitemattributeset *attributeset = [[Cssearchableitemattributeset alloc] initwithitemcontenttype:@       "Views"];
attributeset.title = obj;
attributeset.contentdescription = [NSString stringwithformat:nslocalizedstring (@ "A easy-to-open%@", nil ), obj];
UIImage *thumbimage = [UIImage imagenamed:[nsstring stringwithformat:@ "Icon_%@.png", obj]];
attributeset.thumbnaildata = uiimagepngrepresentation (thumbimage);//beta 1 There is a bug
         Cssearchableitem *item = [[Cssearchableitem alloc] initwithuniqueidentifier:obj                                                                    domainidentifier:@ "Com.kdanmobile.CoreSpotlightDemo"       &nbs p;                                                                Attributeset:attributeset];
[Seachableitems Addobject:item];
}];
[[Cssearchableindex Defaultsearchableindex] Indexsearchableitems:seachableitems
                                                    completionhandler:^ (Nserror * __ Nullable error) {                                                        if (!error)                 &nbs p;                                         
NSLog (@ "%@", error.localizeddescription);
  }];
}  

Execute the next program and then go to spotlight to see if there are any saved in

It doesn't matter if you don't save it, try restarting the emulator and checking it in spotlight. This should be a bug in Xcode
In addition, the meanings of the various properties of Cssearchableitemattributeset are as shown

Figure 6 Demo Spotlight Search

Now we click on the search to the corresponding item can only open our application, if you want to implement the jump also need to take a small step to work: in the Appdelegate implementation

-(BOOL) Application: (nonnull uiapplication *) application continueuseractivity: (nonnull nsuseractivity *) userActivity Restorationhandler: (nonnull void (^) (Nsarray * __nullable)) restorationhandler{

NSString *idetifier = useractivity.userinfo[@ "Kcssearchableitemactivityidentifier"];

Uinavigationcontroller *navigationcontroller = (Uinavigationcontroller *) Self.window.rootViewController;

[Navigationcontroller Poptorootviewcontrolleranimated:yes];

Corespotlighttableviewcontroller *coreviewcontroller = [[Navigationcontroller viewcontrollers] firstObject];

[Coreviewcontroller Showviewcontrollerwithidentifier:idetifier];

return YES;

}

And after implementing this method, clicking on the corresponding item in spotlight will jump to our corresponding viewcontroller.

Figure 7 Demo Spolight Search

The last thing to mention is the deletion of the index. Corespotlight provides us with three ways to remove these:




-(void) Deletesearchableitemswithdomainidentifiers: (nsarray<nsstring *> *) domainidentifiers Completionhandler: (void (^ __nullable) (Nserror * __nullable error)) Completionhandler;

Delete and delete all indexes according to domain according to identifier.
The code from the Xcode7 auto-completion appears to have a lot of previously uncommon code such as: Nonnull, __nullable,nsarray<nsstring *>, etc., where nonull is provided later in a version of Xcode6. The purpose is to better connect with the optional variable in swift. From these it appears that Apple's center of gravity will move slowly towards Swift.

IOS 9 in-app search (corespotlight) API

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.