2015-06-19 86 views
2

我想我的應用程序準備Spotlight索引,所以我有下面的代碼將項目添加到核心聚焦:CoreSpotlight索引工作不因某種原因

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中進行搜索時,什麼都沒有顯示出來。我究竟做錯了什麼?

+0

有時你必須等待OS索引你的項目,尤其是如果有很多。給它時間,停止應用程序,重建並重新在模擬器中運行,你應該沒問題 – soulshined

回答

2

目前Spotlight似乎不適用於某些設備。

你的代碼應該在模擬器上工作。

1

從蘋果iOS 9測試版5個文檔:

- (void)indexSearchableItems:(NSArray<CSSearchableItem *> * _Nonnull)items 
      completionHandler:(void (^ _Nullable)(NSError * _Nullable error))completionHandler 

completionHandler:這意味着該指數使一個音符,它已經當數據已經由指標軸頸這就是所謂的滑輪, 執行這個 操作。如果完成處理程序返回錯誤,則表示 數據未正確記錄,客戶端應該重試 請求。

因此,這意味着,當執行塊,你的代碼登錄到控制檯,該指數已經承認,它需要執行的操作,而不是它已完成索引你的項目。

0
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

可能對你來說已經太遲了。還有就是我的解決方案:

必須保留CSSearchableItem對象,在結束之前
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) { if (!error) NSLog(@"Search item indexed"); }];