My previous comment has wrong format. I can't change it, so I'll write the new one.
I've tried to setup feature in another way - using CSSearchableItemAttributeSet and CSSearchableItem. This is my code:
func save(keywords: [String]) {
guard let bundleId = Bundle.main.bundleIdentifier else { return }
let items = keywords.map {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: UTType.plainText.identifier)
if #available(iOS 18.0, *) {
attributeSet.title = $0
attributeSet.displayName = $0
}
else {
attributeSet.title = $0
attributeSet.displayName = self.appName
}
return CSSearchableItem(
uniqueIdentifier: "\(bundleId).spotlight.\($0)",
domainIdentifier: "\(bundleId).spotlight",
attributeSet: attributeSet
)
}
CSSearchableIndex.default().indexSearchableItems(items) { error in
if let error = error {
print("Spotlight indexing error: \(error.localizedDescription)")
}
else {
print("Spotlight keywords indexed successfully.")
}
}
}
Take a look at the code block if #available(iOS 18.0, *)
. With this code search is working, but not in the way I need - spotlight search displays a couple variants if they are similar and that is understandable, because I set attributeSet.displayName = $0
as one of possible keywords. But if I remove block if #available(iOS 18.0, *) {
and leave just
func save(keywords: [String]) {
guard let bundleId = Bundle.main.bundleIdentifier else { return }
let items = keywords.map {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: UTType.plainText.identifier)
attributeSet.title = $0
attributeSet.displayName = self.appName
return CSSearchableItem(
uniqueIdentifier: "\(bundleId).spotlight.\($0)",
domainIdentifier: "\(bundleId).spotlight",
attributeSet: attributeSet
)
}
CSSearchableIndex.default().indexSearchableItems(items) { error in
if let error = error {
print("Spotlight indexing error: \(error.localizedDescription)")
}
else {
print("Spotlight keywords indexed successfully.")
}
}
}
It won't be working on iOS 18.x versions, but will work on versions lower. Any thoughts?
I'll add information to my FB