Apple documentation on CoreSpotlight sonları:
bir CSSearchableItemAttributeSet nesnesi oluşturun ve dizine eklemek istediğiniz öğeyi açıklayan özelliklerini belirtin.
Öğeyi temsil etmek için CSSearchableItem nesnesi oluşturun. CSSearchableItem nesnesinin, daha sonra 'a başvurmanızı sağlayan benzersiz bir tanımlayıcısı vardır.
Gerekirse, birden fazla öğeyi bir araya getirip bir grup olarak yönetebilmeniz için bir etki alanı tanımlayıcısı belirtin.
Nitelik setini aranabilir öğeyle ilişkilendirin.
Aranabilir öğeyi dizine ekleyin. İşte
Bunu nasıl endeksi basit Not sınıfı gösteren bir örnek aşağıda verilmiştir:
class Note {
var title: String
var description: String
var image: UIImage?
init(title: String, description: String) {
self.title = title
self.description = description
}
}
Sonra diğer bazı işlevinde, oluşturma her not için bir
CSSearchableItemAttributeSet
oluşturmak, notlarınızı oluşturmak nitelik seti ve endeks aranabilir öğeler koleksiyonu gelen
CSSearchableItem
benzersiz:
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)
}
}
Bu örnek arı vardır n, AppCoda's How To Use Core Spotlight Framework in iOS 9 kılavuzundan uyarlanmıştır.
Bu nedenle verileriniz iletilerdir? Şu an için değerler belirleyen özellikler nelerdir? konu veya metin içeriği? – Wain
Neden uygulamanızda .txt dosyasını derinlemesine bağlayamıyorsunuz? Bunun senin durumun için bir hack olacağını biliyorum, ama senin de problemini çözebilirsin. Deeplink için, benzersiz tanımlayıcıyı ayarlayın ve '- (BOOL) uygulamasında alın: (UIApplication *) application continueUserActivity: (nonNull NSUserActivity *) userActivity restorationHandler: (nonnull void (^) (NSArray * _Nullable)) restorationHandler {} AppDelegate.m –
@Wain, veriler kullanıcı tarafından saklanır, dolayısıyla teoride herhangi bir metin olabilir, her zamanki uzunluğu her zaman olabileceği gibi 5000 karakterdir. Başlık ve thumbnailUrl ayarlıyorum, eğer öğe web'den geliyorsa ben de contentSources ayarlıyorum. –