首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于某些原因,CoreSpotlight索引无法工作

由于某些原因,CoreSpotlight索引无法工作
EN

Stack Overflow用户
提问于 2015-06-19 22:45:12
回答 4查看 1.5K关注 0票数 2

我想让我的应用程序为spotlight索引做好准备,所以我有下面的代码来向Core Spotlight添加一个项目:

代码语言:javascript
复制
CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:(NSString *)kUTTypeImage];

attributeSet.title = appName;
attributeSet.contentDescription = appDescription;

attributeSet.keywords = appKeywordsArray;

UIImage *image = [UIImage imageNamed:@"tile-blue.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;

CSSearchableItem *item = [[CSSearchableItem alloc]initWithUniqueIdentifier:appIdentifier domainIdentifier:@"com.myapp" attributeSet:attributeSet];

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {
    if (!error)
        NSLog(@"Search item indexed");
}];

因此,每次运行时,它都会记录Search item indexed,以便在索引过程中不会出现错误。然而,当我在Spotlight中搜索时,什么也没有显示。我做错了什么?

EN

回答 4

Stack Overflow用户

发布于 2015-06-25 23:32:05

目前,Spotlight似乎无法在某些设备上运行。

您的代码应该可以在模拟器上运行。

票数 2
EN

Stack Overflow用户

发布于 2015-08-19 01:48:22

来自苹果iOS 9 Beta 5文档:

代码语言:javascript
复制
- (void)indexSearchableItems:(NSArray<CSSearchableItem *> * _Nonnull)items
           completionHandler:(void (^ _Nullable)(NSError * _Nullable error))completionHandler

completionHandler:当索引记录数据时调用的块,这意味着索引记录了它必须执行此操作。如果完成处理程序返回一个错误,这意味着没有正确记录数据,客户端应该重试请求。

因此,这意味着当代码块被执行,并且您的代码记录到控制台时,索引已经确认它需要执行该操作,而不是它已经完成了对您的项的索引。

票数 1
EN

Stack Overflow用户

发布于 2015-10-08 20:51:33

代码语言:javascript
复制
  Not all devices support CoreSpotlight Search.
  iPhone 4S doesn't support but iPhone 5 does.

if ([CSSearchableIndex isIndexingAvailable]) {
    NSLog(@"Spotlight indexing is available on this device");
    CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];

    attributeSet.title = @"SpotLight Search Demo App";
    attributeSet.contentDescription = @"I am searching this in spotlight";

    attributeSet.keywords = @[@"Search Demo",@"Spotlight"];

    UIImage *image = [UIImage imageNamed:@"Icon.png"];
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
    attributeSet.thumbnailData = imageData;

    CSSearchableItem *item = [[CSSearchableItem alloc]
                              initWithUniqueIdentifier:@"com.suraj"
                              domainIdentifier:@"spotlight.SpotlightSearchDemo"
                              attributeSet:attributeSet];

    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item]
                                                   completionHandler: ^(NSError * __nullable error) {
                                                       if (!error) {
                                                           NSLog(@"Search item is indexed");
                                                       }
                                                   }];
} else {
    NSLog(@"Spotlight indexing is not available on this device");
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30941058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档