我用这个在聚光灯上添加项目,现在我可以在聚光灯下搜索这些项目
但是,当单击聚光灯下的项目时,我如何才能获得详细信息页呢?
我找不到一款迅捷应用生命周期药水。
增加项目:
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我的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如何在用户单击项目外的同时获得正确的项和goto TodoDetail()?
发布于 2022-03-11 02:47:46
您要寻找的是将其添加到一个视图中,该视图具有一个navigationLink,其viewModel包含selectItem变量。
.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)
}
}https://stackoverflow.com/questions/67178125
复制相似问题