我正在测试设备上使用最新版本的Xcode,macOS,iOS(不使用模拟器)和SwiftUI
使用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:
永远不会被调用,因此我无法继续执行它,而是仅通过单击它来显示启动应用程序时的标准行为。任何想法为何?