How to use core spotlight ?

Watched videos, blog post and downloaded their projects and there the core spot lights works accordingly. I copied code to an empty project and did the same as what they did but still is not working os: macOS and iOS

on coredataobject I settled up a attribute to index for spotlight and in object it self I putted the attribute name in display name for spotlight.

static let shared = PersistenceController()
var spotlightDelegate: NSCoreDataCoreSpotlightDelegate?
@MainActor
static let preview: PersistenceController = {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
for _ in 0..<10 {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
}
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()
let container: NSPersistentContainer
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "SpotLightSearchTest")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores(completionHandler: { [weak self] (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
if let description = self?.container.persistentStoreDescriptions.first {
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description.type = NSSQLiteStoreType
if let coordinator = self?.container.persistentStoreCoordinator {
self?.spotlightDelegate = NSCoreDataCoreSpotlightDelegate(
forStoreWith: description,
coordinator: coordinator
)
self?.spotlightDelegate?.startSpotlightIndexing()
}
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
}
}

in my @main view

struct SpotLightSearchTestApp: App {
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.onContinueUserActivity(CSSearchableItemActionType) {_ in
print("")
}
}
}
}

onContinueUserActivity(CSSearchableItemActionType) {_ in print("") } never gets triggered. Sow What am I missing that they dont explain in the blog post or videos ?

How would you know it never gets triggered? You aren't printing anything to the console.

Anyway, in my apps I have the WindowGroup set up like this:

var body: some Scene {
WindowGroup {
PhoneView()
.environment(modelData)
}
}

And then, in the PhoneView() I have this:

var body: some View {
ZStack {
// Whatever...
}
.onContinueUserActivity(CSSearchableItemActionType, perform: handleSpotlight)
// blah blah blah...
}
private func handleSpotlight(userActivity: NSUserActivity) {
guard let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String else { return }
// Handle the activity...
}

I replied to this, but my reply seems to have disappeared... Here we go (again): (And now it's appeared. Apple, issues with the forums after your changes overnight?)

Aside from printing nothing (""), I think you're doing this in the wrong place.

I have this in my main app's WindowGroup. It just loads the PhoneView:

var body: some Scene {
WindowGroup {
PhoneView()
.environment(modelData)
// The difference is that I'm not continuing the Spotlight activity here, unlike you.
}
}
struct PhoneView: View {
var body: some View {
VStack {
// Blah blah blah
}
.onContinueUserActivity(CSSearchableItemActionType, perform: handleSpotlight)
}
private func handleSpotlight(userActivity: NSUserActivity) {
guard let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String else { return }
if(!uniqueIdentifier.isEmpty) {
// Handle the Spotlight activity
}
}
}

I tried it but still never gets triggered. I put a breakpoint on print statement to know if gets triggered. this how my code looks now in content view

NavigationView {
List {
ForEach(items) { item in
NavigationLink {
Text("Item at \(item.text ?? "")")
} label: {
Text(item.timestamp!, formatter: itemFormatter)
}
}
.onDelete(perform: deleteItems)
}
.toolbar {
#if os(iOS)
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
#endif
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
Text("Select an item")
}
.onContinueUserActivity(CSSearchableItemActionType) {_ in
print("IT WORKS")
}
How to use core spotlight ?
 
 
Q