在完成了关于如何将SwiftUI与Handoff结合使用的教程之后,但它对我不起作用。在尝试了很多之后,似乎没有调用OnContinueUserActivity。切换图标显示,但一旦点击它,什么都不会发生。我该怎么解决这个问题?
import SwiftUI
struct Board: View {
@State var BoardId: String
var body: some View {
VStack{
Text("Board: (BoardId)")
}.onContinueUserActivity("bundleIdentifier.Board", perform: { userActivity in
if let Board = userActivity.userInfo?["Board"] as? String {
// Load handoff page
self.BoardId = Board
}
logUserActivity(userActivity, label: "on activity")
})
.userActivity("bundleIdentifier.Board", element: BoardId) {Board, activity in
let bundleid = Bundle.main.bundleIdentifier ?? ""
activity.addUserInfoEntries(from: ["Board": BoardId,
"setby": bundleid])
logUserActivity(activity, label: "activity")
}
}
}
struct Board_Previews: PreviewProvider {
static var previews: some View {
Board(BoardId: "NaN")
}
}
func logUserActivity(_ activity: NSUserActivity, label: String = "") {
print("(label) TYPE = (activity.activityType)")
print("(label) INFO = (activity.userInfo ?? [:])")
}
您仍然可以在SwiftUI和WidgetKit中使用onContinueUserActivity。如果您将您的并发症定义为:
@main
struct My_Complication: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(kind: "com.mycompany.myapp.complication",
provider: Provider()) { entry in
MyEntryView(entry: entry)
}
.configurationDisplayName(...)
.description(...)
}
}
然后在onContinueUserActivity
中使用相同的kind
标识符,如下所示:
.onContinueUserActivity("com.mycompany.myapp.complication", perform: { _ in
// Do something for complication
})
对于将来遇到这种情况的人,swiftui新实现了从通用链接(或深度链接(接收url的方法它不再是基于continue useractivity
的方法
@main
struct YouApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
print(url)
}
}
}
}
像这样的东西。