我想让我的应用程序为spotlight索引做好准备,所以我有下面的代码来向Core Spotlight添加一个项目:
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中搜索时,什么也没有显示。我做错了什么?
发布于 2015-06-25 23:32:05
目前,Spotlight似乎无法在某些设备上运行。
您的代码应该可以在模拟器上运行。
发布于 2015-08-19 01:48:22
来自苹果iOS 9 Beta 5文档:
- (void)indexSearchableItems:(NSArray<CSSearchableItem *> * _Nonnull)items
completionHandler:(void (^ _Nullable)(NSError * _Nullable error))completionHandlercompletionHandler:当索引记录数据时调用的块,这意味着索引记录了它必须执行此操作。如果完成处理程序返回一个错误,这意味着没有正确记录数据,客户端应该重试请求。
因此,这意味着当代码块被执行,并且您的代码记录到控制台时,索引已经确认它需要执行该操作,而不是它已经完成了对您的项的索引。
发布于 2015-10-08 20:51:33
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");
} https://stackoverflow.com/questions/30941058
复制相似问题