Search code examples
swiftswiftuihandoffnsuseractivity

application:continueUserActivity:restorationHandler: is not getting called when using handoff


I am using the newest versions of Xcode, macOS, iOS on my test devices (not using the simulator) and SwiftUI

My UI with SwiftUI and my 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)
        }
    }
}

Inside my appdelegate:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    print("this is not getting called")
    return true
}

When I start the app on one device and use my other device to go into that menu (when double clicking the home button) and click the handoff symbol of my test app at the bottom of the screen, the app gets launched but the function application:continueUserActivity:restorationHandler: never gets called so I can't proceed instead it shows the standard behavior when launching the app by just clicking it. Any ides why that is?


Solution

  • It might be a bug this post reports the same issue and still has no answer so I would assume this might be a bug with the beta version of Xcode.