Skip to content

Instantly share code, notes, and snippets.

@d-date
Last active December 14, 2020 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-date/f13e501959d44bae475ed0c20c81190b to your computer and use it in GitHub Desktop.
Save d-date/f13e501959d44bae475ed0c20c81190b to your computer and use it in GitHub Desktop.
Core Spotlight
import CoreSpotlight
import Foundation
#if canImport(UniformTypeIdentifiers)
import UniformTypeIdentifiers
#endif
import MobileCoreServices
struct ShopIndexHandler {
static let `default` : ShopIndexHandler = .init()
private let spotlight: CSSearchableIndex = .default()
private init() {}
static func add(shop: ShopSummary) {
let attributeSet: CSSearchableItemAttributeSet = {
if #available(iOS 14.0, *) {
return CSSearchableItemAttributeSet(contentType: .text)
} else {
return CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
}
}()
attributeSet.title = shop.brand.name + " " + shop.name
if let imageUrl = shop.imageUrls.first {
attributeSet.thumbnailURL = URL(string: imageUrl)
}
attributeSet.latitude = NSNumber(value: shop.latitude)
attributeSet.longitude = NSNumber(value: shop.longitude)
attributeSet.contentDescription = shop.scheduleText
let item = CSSearchableItem(uniqueIdentifier: "\(shop.id)", domainIdentifier: nil, attributeSet: attributeSet)
CSSearchableIndex.default().indexSearchableItems([item]) { error in
if let error = error {
print(error.localizedDescription)
} else {
print("Shop indexed.")
}
}
}
}
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
// Handle unique identifier
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment