Last active
December 14, 2020 06:02
-
-
Save d-date/f13e501959d44bae475ed0c20c81190b to your computer and use it in GitHub Desktop.
Core Spotlight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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