我在我的测试设备(不使用模拟器)和 SwiftUI 上使用最新版本的 Xcode、macOS、iOS
我的 SwiftUI 用户界面和我的 UserActivity DelegateHandler:
struct UserActivityDelegateObserver: UIViewRepresentable {
@Binding var textData: String
func makeUIView(context: Context) -> UIView { return UIView() }
func updateUIView(_ uiView: UIView, context: Context) {
context.coordinator.userActivity?.needsSave = true
}
func makeCoordinator() -> Coordinator {
Coordinator(textData: _textData)
}
class Coordinator: UIResponder, NSUserActivityDelegate {
@Binding var textData: String
init(textData: Binding<String>) {
self._textData = textData
super.init()
self.userActivity = NSUserActivity(activityType: "com.domain.appname.activity")
self.userActivity!.title = "My Activity"
self.userActivity!.userInfo = ["Key": textData.wrappedValue]
self.userActivity!.isEligibleForHandoff = true
self.userActivity!.becomeCurrent()
}
override func updateUserActivityState(_ activity: NSUserActivity) {
activity.addUserInfoEntries(from: ["Key": textData])
super.updateUserActivityState(activity)
}
}
}
struct ContentView: View {
@State var textData: String = "Placeholder"
var body: some View {
NavigationView {
ZStack {
UserActivityDelegateObserver(textData: $textData)
Form {
Section {
TextField("Placeholder", text: $textData)
}
}
}
.navigationBarTitle("Title", displayMode: .inline)
}
}
}
在我的里面appdelegate
:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
print("this is not getting called")
return true
}
当我在一台设备上启动应用程序并使用我的另一台设备进入该菜单(双击主页按钮时)并单击屏幕底部我的测试应用程序的切换符号时,应用程序启动但该功能application:continueUserActivity:restorationHandler:
永远不会被调用,所以我无法继续,而是通过单击它来启动应用程序时显示标准行为。任何想法为什么会这样?