#iOS开发笔记#如何在iOS9中实现应用内搜索(Objective-C)

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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值