In the Mail app or in the Messages app you can search for the content of any message using the Core Spotlight search. Also I can see OneNote doing this, so it should be available in APIs.
However, documentation about that is almost none existent. I can see only that in CSSearchableItemAttributeSet
there is contentUrl
, but I have tried to set NSUrl of the .txt file and nothing happened. Also tried to set contentType to kUTTypeText
and kUTTypeUTF8PlainText
but no improvements.
Is some specific file format required? Or something else one should do?
Core Spotlight enables you to index content at any point, such as when the app loads, and the Core Spotlight APIs don't require users to visit the content in order to index it. Core Spotlight works best when you have no more than a few thousand items.
Spotlight is a system-wide desktop search feature of Apple's macOS and iOS operating systems. Spotlight is a selection-based search system, which creates an index of all items and files on the system.
Spotlight can help you quickly find apps, documents, emails, and other items on your Mac. With Siri Suggestions, you can also get news, sports scores, weather conditions, stock prices, and more. Spotlight can even perform calculations and conversions for you.
The Apple documentation on CoreSpotlight breaks down the process of creating and adding items to a searchable index:
Create a CSSearchableItemAttributeSet object and specify properties that describe the item you want to index.
Create a CSSearchableItem object to represent the item. A CSSearchableItem object has a unique identifier that lets you refer to it later.
If needed, specify a domain identifier so that you can gather multiple items together and manage them as a group.
Associate the attribute set with the searchable item.
Add the searchable item to the index.
Here's a quick example I that shows how to index a simple Note class:
class Note {
var title: String
var description: String
var image: UIImage?
init(title: String, description: String) {
self.title = title
self.description = description
}
}
Then in some other function, create your notes, create a CSSearchableItemAttributeSet
for each note, create a unique CSSearchableItem
from the attribute set, and index the collection of searchable items:
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)
}
}
This example has been adapted from AppCoda's How To Use Core Spotlight Framework in iOS 9 guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With