Reputation: 1594
I want to get my app ready for spotlight indexing, so I've got the code below to add an item to 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");
}];
So, every time this runs, it logs Search item indexed
so there are no errors during the indexing. However, when I go and search in Spotlight, nothing shows up. What am I doing wrong?
Upvotes: 3
Views: 1784
Reputation: 590
May be it's too late for you. There is my solution:
you must retain CSSearchableItem
object, before you finish
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {
if (!error)
NSLog(@"Search item indexed");
}];
Upvotes: 0
Reputation: 1401
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");
}
Upvotes: 0
Reputation: 5365
From Apple iOS 9 Beta 5 docs:
- (void)indexSearchableItems:(NSArray<CSSearchableItem *> * _Nonnull)items
completionHandler:(void (^ _Nullable)(NSError * _Nullable error))completionHandler
completionHandler: The block that’s called when the data has been journaled by the index, which means that the index makes a note that it has to perform this operation. If the completion handler returns an error, it means that the data wasn’t journaled correctly and the client should retry the request.
So this means that when the block is executed, and your code logs to console, the index has acknowledged that it needs to perform that operation and not that it has finished indexing your items.
Upvotes: 2
Reputation: 2722
At the moment Spotlight seems to not work on some devices.
Your code should work on the simulator.
Upvotes: 2