2016-07-25 62 views
5

在Mail應用程序或Messages應用程序中,您可以使用Core Spotlight搜索搜索任何消息的內容。我也可以看到OneNote這樣做,所以它應該在API中可用。使用Core Spotlight爲內容編制索引

但是,有關這方面的文檔幾乎不存在。我只能看到在CSSearchableItemAttributeSetcontentUrl,但我試圖設置.txt文件的NSUrl並沒有發生任何事情。還嘗試將contentType設置爲kUTTypeTextkUTTypeUTF8PlainText,但沒有任何改進。

是否需要某些特定的文件格式?或者其他人應該做的?

+0

所以你的數據是消息?你目前設置了哪些屬性值?主題或文字內容? – Wain

+0

爲什麼你不能只是深入鏈接你的應用程序中的.txt文件。我知道這對你的情況是一種破解,但也可以解決你的問題。爲了進行深度鏈接,請設置唯一標識符並將其放入' - (BOOL)應用程序中:(UIApplication *)application continueUserActivity :(nonnull NSUserActivity *)userActivity restorationHandler :(nonnull void(^)(NSArray * _Nullable))restorationHandler {}'in AppDelegate.m –

+0

@Wain,數據是由用戶存儲的,理論上它可以是任何文本,它的通常長度大約是5000個字符,儘管它可以是任意長度。我設置標題和thumbnailUrl,如果項目來自網絡我也設置contentSources。 –

回答

6

Apple documentation on CoreSpotlight擊穿創建和添加項目到搜索索引的過程:

  • 創建CSSearchableItemAttributeSet對象,並指定描述要編制索引的項目屬性。

  • 創建一個CSSearchableItem對象來表示該項目。一個CSSearchableItem對象有一個唯一的標識符,可以讓你稍後參考 。

  • 如果需要,指定一個域標識符,以便您可以將多個項目收集在一起並作爲一個組進行管理。

  • 將屬性集與可搜索項關聯。

  • 將可搜索項目添加到索引。

下面是一個簡單的例子,我表示指數如何將簡單的註釋類:

class Note { 
    var title: String 
    var description: String 
    var image: UIImage? 

    init(title: String, description: String) { 
     self.title = title 
     self.description = description 
    } 
} 

然後在其他一些功能,創建筆記,爲每個音符CSSearchableItemAttributeSet,創建從屬性集,和索引搜索項的集合獨特CSSearchableItem

import CoreSpotlight 
import MobileCoreServices 

// ... 

// Build your Notes data source to index 
var notes = [Note]() 
notes.append(Note(title: "Grocery List", description: "Buy milk, eggs")) 
notes.append(Note(title: "Reminder", description: "Soccer practice at 3")) 
let parkingReminder = Note(title: "Reminder", description: "Soccer practice at 3") 
parkingReminder.image = UIImage(named: "parkingReminder") 
notes.append(parkingReminder) 

// The array of items that will be indexed by CoreSpotlight 
var searchableItems = [CSSearchableItem]() 

for note in notes { 
    // create an attribute set of type Text, since our reminders are text 
    let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String) 

    // If we have an image, add it to the attribute set 
    if let image = note.image { 
     searchableItemAttributeSet.thumbnailData = UIImagePNGRepresentation(image) 
     // you can also use thumbnailURL if your image is coming from a server or the bundle 
//  searchableItemAttributeSet.thumbnailURL = NSBundle.mainBundle().URLForResource("image", withExtension: "jpg") 
    } 

    // set the properties on the item to index 
    searchableItemAttributeSet.title = note.title 
    searchableItemAttributeSet.contentDescription = note.description 

    // Build your keywords 
    // In this case, I'm tokenizing the title of the note by a space and using the values returned as the keywords 
    searchableItemAttributeSet.keywords = note.title.componentsSeparatedByString(" ") 

    // create the searchable item 
    let searchableItem = CSSearchableItem(uniqueIdentifier: "com.mygreatapp.notes" + ".\(note.title)", domainIdentifier: "notes", attributeSet: searchableItemAttributeSet) 
} 

// Add our array of searchable items to the Spotlight index 
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) in 
    if let error = error { 
     // handle failure 
     print(error) 
    } 
} 

這個例子有蜜蜂n改編自AppCoda's How To Use Core Spotlight Framework in iOS 9指南。

+0

我有一些評論,但刪除了他們並完成了實驗。在不是那麼短的電子郵件(18600個字符)中,每個單詞都是可搜索的,包括像'this'這樣的常見單詞。我相信它不包括使用描述,並且很可能也排除了關鍵字。你同意嗎? –

+0

@IvanIčinOneNote有可能在創建可搜索項目的索引之前從可搜索文本中剝離代詞或其他被列入黑名單的單詞列表。 – JAL

相關問題