IOS9 Series two--the new search function API

Source: Internet
Author: User

A more intelligent search Solution--ios9 search function New API
First, Introduction

IOS9 has provided us with a number of new APIs, and the enhancement of search functionality is undoubtedly one of the more prominent. First, let's imagine: if you define an identifier in your app, can you use that identifier to search for your app in Siri and search, isn't it great? No, that's far too much, you can define arbitrary data so that it can be quickly retrieved in search and Siri, so is the search feature cool? No, there are more cool, you can even add some logo to your website, so that Apple crawlers can retrieve, so that even if the user does not install your app, you can also get the appropriate information in the search, it is too powerful, right.

ii. 3 new search Modes
?1, nsuseractivity?

We can use the corresponding function in the project to add some active elements of the user, so that we can show our app by searching for such active elements in our search. For example:

Create an object, where the type is used to differentiate between types of searches nsuseractivity *useractivity = [[Nsuseractivity alloc] Initwithactivitytype: @ "MyApp"];    The caption displayed useractivity.title = @ "My app";    Search keywords useractivity.keywords = [nsset setwitharray: @[@ "Sea", @ "RCH"];    Support Search Useractivity.eligibleforsearch = YES; Commit settings [useractivity becomecurrent];

In the following function, we can handle the callback after the user clicks the search:

-(BOOL) Application: (UIApplication *) application continueuseractivity: (nsuseractivity *) useractivity    restorationhandler:{nsstring *activitytype = Useractivity.activitytype;         if ([Activitytype isequal: @ "MyApp"]) {//Handle restoration for values provided in UserInfo//do something    return YES;    } return NO; Handling Callbacks}

TIP: Keyword Searches added in this way, you must create global variables, or you cannot search:

2, Corespotlight

Corespotlight is a more liberal way to search, by adding a model like item to show the data in the app in the search bar, the Corespotlight framework provides some actions for adding, deleting, changing, and checking, but it allows us to set the search properties freely.

(1) Recognition of 3 classes

In IOS9, 3 new classes were added, and with the operation and coordination of these three classes, we can easily add corespotlight search functionality to the app.

Cssearchableitemattributeset: Sets the class, which is used to set the icon, content, image, etc. in the search tag. The main usage is as follows:

The core method of this class has only one Init method, created by a type string, used to differentiate @interface cssearchableitemattributeset :  in callbacks nsobject <nscopying,nssecurecoding>-  (Instancetype) Initwithitemcontenttype: (nonnull  nsstring *) Itemcontenttype; @end//More property settings in its extension class, for example: @interface  CSSearchableItemAttributeSet  ( csgeneral)//Display the name @property (nullable, copy)  nsstring *displayname;//the name Array @property (nullable,  copy)  nsarray<nsstring*> *alternatenames;//complete path @property (nullable, copy)   nsstring *path;//link Url@property (nullable, strong)  nsurl *contenturl;//image Link Url@property ( Nullable, strong)  nsurl *thumbnailurl;//Set picture data @property (nullable, copy)  NSData * thumbnaildata;//set an identifier @property (nullable, copy)  NSString *relatedUniqueIdentifier; @property ( Nullable, strong)  nsdate *metadatamodificationdate;//Content Type @property (nullable, copy)   nsstring *contenttype; @property (nullable, copy)  nsarray<nsstring*> *contenttypetree;//Search for the key word group @property (nullable, copy)  nsarray<nsstring*> *keywords;//title Information @property (nullable, copy)  NSString *title;@ End


cssearchableitem: Search Tag class, through this class, to create a response search label. The main contents are as follows:

This class is primarily used to create search labels @interface cssearchableitem : nsobject <nssecurecoding, nscopying >//init method-  (Instancetype) Initwithuniqueidentifier: (nullable nsstring *) UniqueIdentifier  //Can be null, one will be generated                          Domainidentifier: (nullable nsstring *) domainidentifier                              attributeset: (cssearchableitemattributeset *) attributeset;//the corresponding   properties @property  ( copy)  NSString *uniqueIdentifier; @property   (copy, nullable)  nsstring * Domainidentifier, @property   (copy, null_resettable)  NSDate * expirationDate; @property   (Strong)  cssearchableitemattributeset *attributeset; @end 

Cssearchableindex: This class, I personally understand, similar to the role of a manager, through it to the label to add, delete, change, check and other operations:

@interface  CSSearchableIndex : NSObject@property  (weak,nullable)  id< cssearchableindexdelegate> indexdelegate;//determine if the device supports +  (BOOL) isindexingavailable;// Searchindex Manager +  (instancetype) defaultsearchableindex;//in general, we do not need to recreate objects-  (instancetype) Initwithname: (nsstring *) name;-  (instancetype) Initwithname: (nsstring *) name  Protectionclass: (nullable nsstring *) protectionclass;//set index label-  (void) Indexsearchableitems: ( nsarray<cssearchableitem *> *) Items completionhandler: (void  (^ __nullable) ( nserror * __nullable error)) completionhandler;//Delete the specified ID index label-  (void) Deletesearchableitemswithidentifiers: (nsarray<nsstring *> *) Identifiers completionhandler :(void  (^ __nullable) (Nserror * __nullable error)) completionhandler;-  (void) Deletesearchableitemswithdomainidentifiers: (nsarray<nsstring *> *) DomainIdentifierS completionhandler: (void  (^ __nullable) (Nserror * __nullable error)) completionhandler;//Delete all index labels-  (void) Deleteallsearchableitemswithcompletionhandler: (void  (^ __ Nullable) (Nserror * __nullable error)) Completionhandler; @end
(2) A small example

Below, we use a small example to apply the search function under Corespotlight.

First, you need to import the following libraries into your project:

Implement the following code:

Label Settings Cssearchableitemattributeset * itemset = [[Cssearchableitemattributeset alloc]initwithitemcontenttype:@]    MyApp "];    Itemset.title = @ "My app";    Itemset.keywords = @[@ "haha", @ "123"];    Itemset.contentdescription = @ "This is the search for content";        Itemset.thumbnaildata = Uiimagepngrepresentation ([UIImage imagenamed:@ "Icon-114.png"]); Cssearchableitem * item = [[Cssearchableitem alloc]initwithuniqueidentifier:@ "1" domainidentifier:@ "1" AttributeSet:        Itemset]; [[Cssearchableindex Defaultsearchableindex]indexsearchableitems:@[item] completionhandler:nil];

We enter haha or 123 in the search for the following results:

Corespotlight's search callbacks, like Nsuseractivaty, differ only in the way that IDs are differentiated:

-  (BOOL) Application: (uiapplication *) Application continueuseractivity: (NSUserActivity *) useractivity restorationhandler:{    nsstring *activitytype =  useractivity.activitytype;    //First Fetch cssearchableitemactiontype    if   ([Activitytype isequal: cssearchableitemactiontype])  {         NSString *uniqueIdentifier = [userActivity.userInfo                                                    objectForKey:CSSearchableItemActivityIdentifier];         // do something         return yes;  &nbSp; }    return no;} 
3. Web Markup

This feature is not relevant to our app development, but the promotion of my app is very important, this technology can let our app associated with a website, Apple through the crawler to get some of our specified tag values, regardless of whether the user installed the app, in search, can show the relevant information, Because this feature is primarily associated with front-end technology, you need to know what to see:App Search ProgrammingGuide.

Third, conclusion

In many of the related articles I refer to, all agree that IOS9 's search function is powerful, but abuse will cause the flood of spam information, such results will be counterproductive, as a developer, we need to be the most appropriate, the most concise information pushed to the front of the user. In addition, the article has omissions and mistakes, welcome to correct.


Welcome reprint please indicate the source





IOS9 Series two--the new search function 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.