IOS9-CoreSpotlight到底怎么用?(OC语言版)

什么是CoreSpotlight?简单的说:就是在IOS9下,让用户在下拉的搜索页面里可以搜索到你的应用。

#import <Foundation/Foundation.h>

@interface IOS9SearchAPIUtil : NSObject
+(IOS9SearchAPIUtil *)sharedInstance;
-(void)addSearchItemsArray:(NSArray *)array;
@end

#import "IOS9SearchAPIUtil.h"
#import <CoreSpotlight/CoreSpotlight.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <UIKit/UIKit.h>
BOOL IS_IOS_9 = NO;
#define IOS9SearchAPIUtil_domainIdentifier @"com.searchapi.shows"
@implementation IOS9SearchAPIUtil

+(IOS9SearchAPIUtil *)sharedInstance{
    static IOS9SearchAPIUtil *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

-(void)addSearchItemsArray:(NSArray *)array{
    if ([[UIDevice currentDevice].systemVersion floatValue]>=9.0) {
        IS_IOS_9 = YES;
    }
    
    if(IS_IOS_9){
        NSMutableArray <CSSearchableItem *> *searchableItems = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *d in array) {
            NSString *title = [d objectForKey:@"title"];
            NSString *desc = [d objectForKey:@"desc"];
            NSString *time = [d objectForKey:@"time"];
            NSString *nid = [d objectForKey:@"nid"];
            
            NSLog(@"d----%@",d);
            CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:@"SearchAPIViews"];
            attributeSet.title = title;
            attributeSet.contentDescription = [NSString stringWithFormat:@"%@\n%@",desc,time];
//            NSMutableArray *keywords = [NSMutableArray arrayWithArray:[title split:@" "]];
            NSMutableArray *keywords = [NSMutableArray arrayWithArray:[title componentsSeparatedByString:@" "]];
            [keywords addObject:desc];
            attributeSet.keywords = keywords;
            NSString *identifiner = [NSString stringWithFormat:@"%@",nid];
            
            //应该是唯一的,以你的应用程序组。
            // REQUIRED因为这是将参考项的方式来更新索引/从索引中删除
            //首先是一个UUID,易于使用的,但你可以用自己的UID更换之前,如果你希望该项目第一个索引。
            [searchableItems addObject:[[CSSearchableItem alloc]initWithUniqueIdentifier:identifiner domainIdentifier:IOS9SearchAPIUtil_domainIdentifier attributeSet:attributeSet]];
        }
        
        [[CSSearchableIndex defaultSearchableIndex]indexSearchableItems:searchableItems completionHandler:^(NSError * __nullable error) {
            if(error != nil){
                NSLog(@"%@",error.localizedDescription);
            }else {
                NSLog(@"Items were indexed successfully");
            }
        }];
    }
}

@end

#import "AppDelegate.h"
#import "IOS9SearchAPIUtil.h"


@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //添加数据源
    
    [[IOS9SearchAPIUtil sharedInstance]addSearchItemsArray:@[@{@"nid":@"fin://1238796",@"title":@"fin",@"desc":@"基金",@"time":@"2014-01-01"},@{@"nid":@"fun://54fdsaf32",@"title":@"fun",@"desc":@"理财",@"time":@"2016-01-01"}]];
    
    return YES;
}


                           


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值