的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指南。
來源
2016-07-27 14:59:09
JAL
所以你的數據是消息?你目前設置了哪些屬性值?主題或文字內容? – Wain
爲什麼你不能只是深入鏈接你的應用程序中的.txt文件。我知道這對你的情況是一種破解,但也可以解決你的問題。爲了進行深度鏈接,請設置唯一標識符並將其放入' - (BOOL)應用程序中:(UIApplication *)application continueUserActivity :(nonnull NSUserActivity *)userActivity restorationHandler :(nonnull void(^)(NSArray * _Nullable))restorationHandler {}'in AppDelegate.m –
@Wain,數據是由用戶存儲的,理論上它可以是任何文本,它的通常長度大約是5000個字符,儘管它可以是任意長度。我設置標題和thumbnailUrl,如果項目來自網絡我也設置contentSources。 –