#iOS开发笔记 # How to implement an in-app search in iOS9 (objective-c)

Source: Internet
Author: User


IOS9 has added a lot of new features (What ' New in IOS 9.0-apple Developer), and the search feature is interesting to me. In a nutshell, there are three different ways to implement local and server two searches.



Try it yourself. The second is to use the core Spotlight framework to implement the index APP Content. The main steps are as follows:



1. Add the required framework



Requires two framework:corespotlight and mobilecoreservices, which can be directly @import (the difference between @import and #import: @import vs #import), This allows you to not add the framework manually in your project.


@import corespotlight;
@import mobilecoreservices;

2. Create Cssearchableitemattributeset
Cssearchableitemattributeset *myattributeset = [[Cssearchableitemattributeset alloc] Initwithitemcontenttype: ( NSString *) Kuttypedata];
    The title shown in the search results
    myattributeset.title = @ "Apple";
    The details displayed in the search results
    myattributeset.contentdescription = @ "A red apple";
    Keywords, when the user input the following string, the relevant content will be retrieved
    myattributeset.keywords = [Nsarray arraywithobjects:@ "Hello", @ "Search", @ "Fruit", NIL];

3. Create a searchable entry Cssearchableitem
Initializes a searchable entry
    cssearchableitem *item = [[Cssearchableitem alloc] initwithuniqueidentifier:@ "apple-001" domainidentifier:@ "xxx.fruit.com" attributeset:myattributeset];

4. Add Search Entry
Add Portal
    [[Cssearchableindex Defaultsearchableindex] Indexsearchableitems:@[item] completionhandler:^ (NSError * _ Nullable Error {
        if (error) {
            NSLog (@ "Search item failed to is indexed");
        } else {
            NSLog (@ "Search item in Dexed ");
        }
    ;


5. There are three ways to delete the search portal by deleting the search portal, based on Uniqueid,domain and deleting all portals
[[Cssearchableindex Defaultsearchableindex] Deletesearchableitemswithidentifiers:[nsarray arrayWithObjects:@ " apple-001 ", @" apple-002 ", Nil] completionhandler:^ (Nserror * _nullable error) {
        if (!error) {
            NSLog (@" Items deleted ");
        }
    ;
    
    [[Cssearchableindex Defaultsearchableindex] Deletesearchableitemswithdomainidentifiers:[nsarray arrayWithObjects: @ "xxx.fruit.com", Nil] completionhandler:^ (Nserror * _nullable error) {
        if (!error) {
            NSLog (@ "Items deleted"); c7/>}
    }];
    
    [[Cssearchableindex Defaultsearchableindex] deleteallsearchableitemswithcompletionhandler:^ (NSError * _Nullable Error) {
        if (!error) {
            NSLog (@ "All items deleted");
        }
    ];

6. Full example
//
// FirstViewController.m
// Hello Search

#import "FirstViewController.h"
@import CoreSpotlight;
@import MobileCoreServices;

@interface FirstViewController ()

@end

@implementation FirstViewController

-(void) viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self initSearchableIndex];
}

-(void) didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) initSearchableIndex {
    
    CSSearchableItemAttributeSet * myAttributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType: (NSString *) kUTTypeData];
    // title displayed in search results
    myAttributeSet.title = @ "Apple";
    // Details shown in search results
    myAttributeSet.contentDescription = @ "A red apple";
    // keyword, when the user enters the following string, the relevant content will be retrieved
    myAttributeSet.keywords = [NSArray arrayWithObjects: @ "Hello", @ "Search", @ "Fruit", nil];
    
    // initialize a searchable entry
    CSSearchableItem * item = [[CSSearchableItem alloc] initWithUniqueIdentifier: @ "apple-001" domainIdentifier: @ "xxx.fruit.com" attributeSet: myAttributeSet];
    
    // add entry
    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems: @ [item] completionHandler: ^ (NSError * _Nullable error) {
        if (error) {
            NSLog (@ "Search item failed to be indexed");
        } else {
            NSLog (@ "Search item indexed");
        }
    }];
}

-(void) deleteSearchableIndex {
    [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers: [NSArray arrayWithObjects: @ "apple-001", @ "apple-002", nil] completionHandler: ^ (NSError * _Nullable error) {
        if (! error) {
            NSLog (@ "Items deleted");
        }
    }];
    
    [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers: [NSArray arrayWithObjects: @ "xxx.fruit.com", nil] completionHandler: ^ (NSError * _Nullable error) {
        if (! error) {
            NSLog (@ "Items deleted");
        }
    }];
    
    [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler: ^ (NSError * _Nullable error) {
        if (! error) {
            NSLog (@ "All items deleted");
        }
    }];
}

@end 





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.