我使用它在聚光灯上添加项目,现在我可以在聚光灯上搜索这些项目
但是当我点击聚光灯下的项目时如何进入详细信息页面?
我找不到 swiftui-app 生命周期解决方案
添加项目:
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
我的应用程序.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
当用户单击外侧的项目时,如何获得正确的项目并转到 TodoDetail() ?
您正在寻找的是将其添加到具有 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://fatbobman.medium.com/display-core-data-in-spotlight-within-the-app-df4b981f3b02