Reputation: 385
I'm using this to add item on spotlight , and now i can search these item on spotlight
but how can i get to detail page when click the item on spotlight ?
I can not found a swiftui-app lifecycle sulotion
add item :
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeContact as String)
attributeSet.title = task
attributeSet.contentDescription = task
attributeSet.relatedUniqueIdentifier = uuid.uuidString
attributeSet.identifier = uuid.uuidString
attributeSet.addedDate = Date()
let searchableItem = CSSearchableItem(uniqueIdentifier: uuid.uuidString, domainIdentifier: aType, attributeSet: attributeSet)
CSSearchableIndex.default()
.indexSearchableItems([searchableItem]) { error in
if let error = error {
print("Error indexing: \(error)")
} else {
print("Indexed.")
} // error
} // .indexSearchableItems
my app.swift
import SwiftUI
import Intents
import CoreSpotlight
import CoreServices
let aType = "com.example.icecream-selection"
@main
struct DevoteApp: App {
let persistenceController = PersistenceController.shared
let nsUserActivity: NSUserActivity = NSUserActivity(activityType: aType)
@AppStorage("isDarkMode") var isDarkMode: Bool = false
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
// CSSearchableItemActionType
if nsUserActivity.activityType == CSSearchableItemActionType {
if (nsUserActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String) != nil {
let context = PersistenceController.shared.container.viewContext
let newItem = Item(context: context)
TodoDetail(item:newItem)
} // nil
} else {
ContentView()
// IceContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.preferredColorScheme(isDarkMode ? .dark : .light)
.onOpenURL { url in
print("Received URL: \(url)")
}
.onContinueUserActivity( CSSearchableItemActionType) { userActivity in
if let color = userActivity.persistentIdentifier {
print(">>>>>>>SUCCESS ACTIVITY<<<<<<<")
print(color)
}
}
} // CSSearchableItemActionType
} // WindowGroup
.onChange(of: scenePhase) { newScenePhase in
switch newScenePhase {
case .active:
print("App is active")
print("KEY TYPE")
print(nsUserActivity.activityType)
print(CSSearchableItemActionType)
case .inactive:
print("App is inactive")
case .background:
print("App is in background")
@unknown default:
print("Oh - interesting: I received an unexpected new value.")
} // newScenePhase
} // .onChange
} // Scene
} // App
and how can i get the right item and goto TodoDetail() while user click the item out side ?
Upvotes: 1
Views: 637
Reputation: 63
What you are looking for is to add this to one of your views that has a navigationLink with its viewModel containing the selectItem variable.
.onContinueUserActivity(CSSearchableItemActionType, perform: loadItem)
func loadItem(_ userActivity: NSUserActivity) {
if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
// You need to have a NavigationLink that you has the selection set to selectItem with it's tag being the uniqueIdentifier to trigger to navigation.
viewModel.selectItem(with: uniqueIdentifier)
}
}
Upvotes: 1