#iOS開發筆記#如何在iOS9中實現應用程式內搜尋(Objective-C)

來源:互聯網
上載者:User

iOS9中增加了許多新特性(What's New in iOS 9.0 - Apple Developer),其中的Search功能讓我高度興趣。簡單看了一下,主要有三種方式,來實現local和server兩種搜尋。

自己動手嘗試了下第二種,即用Core Spotlight架構來實現Index App Content。主要步驟如:

1. 添加所需要的架構

需要兩個framework:CoreSpotlight和MobileCoreServices,可以直接@import(有關@import和#import的區別:@import vs #import),這樣就不用在項目中手工添加framework。

@import CoreSpotlight;@import MobileCoreServices;

2. 建立 CSSearchableItemAttributeSet
CSSearchableItemAttributeSet *myAttributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeData];    // 搜尋結果中顯示的標題    myAttributeSet.title = @"Apple";    // 搜尋結果中顯示的詳情    myAttributeSet.contentDescription = @"A red apple";    // 關鍵詞,當使用者輸入以下字串時,相關內容會被檢索到    myAttributeSet.keywords = [NSArray arrayWithObjects:@"Hello", @"Search", @"Fruit", nil];

3. 建立可檢索條目 CSSearchableItem
// 初始化一個可檢索的條目    CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"apple-001" domainIdentifier:@"xxx.fruit.com" attributeSet:myAttributeSet];

4. 添加檢索入口
// 添加入口    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) {        if (error) {            NSLog(@"Search item failed to be indexed");        } else {            NSLog(@"Search item indexed");        }    }];


5. 刪除檢索入口 有三種方式來刪除檢索入口,分別是根據uniqueID,domain以及刪除所有入口
[[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");        }    }];

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];    // 搜尋結果中顯示的標題    myAttributeSet.title = @"Apple";    // 搜尋結果中顯示的詳情    myAttributeSet.contentDescription = @"A red apple";    // 關鍵詞,當使用者輸入以下字串時,相關內容會被檢索到    myAttributeSet.keywords = [NSArray arrayWithObjects:@"Hello", @"Search", @"Fruit", nil];        // 初始化一個可檢索的條目    CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"apple-001" domainIdentifier:@"xxx.fruit.com" attributeSet:myAttributeSet];        // 添加入口    [[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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.