CoreSpotlight indexing not working for some reason

1.6k views Asked by At

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?

4

There are 4 answers

0
KIDdAe On

At the moment Spotlight seems to not work on some devices.

Your code should work on the simulator.

0
Nicolas S On

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.

0
Ryan Yan On

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"); }];

0
Suraj Mirajkar On
  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");
}