onContinueUserActivity not Called with Handoff & SwiftUI

886 views Asked by At

After following this tutorial about how to use SwiftUI in combination with Handoff, but it isn't working for me. After trying a lot it seems like the OnContinueUserActivity is not called. The icon for handoff shows but once clicked on it, nothing happens. How can I fix this?

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 ?? [:])")
}


2

There are 2 answers

0
MartianMartian On

for people come across this in the future, swiftui has newly implemented approach for receiving url from universal links(or, deep links) it's no longer continue useractivity based approach

@main
    struct YouApp: App {
      var body: some Scene {
        WindowGroup {
          ContentView()
            .onOpenURL { url in
              print(url)
            }
        }
      }
    }

something like this.

0
Oded Ben Dov On

You can still use onContinueUserActivity in SwiftUI and WidgetKit. If you defined your complication as such:

@main
struct My_Complication: Widget {    
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: "com.mycompany.myapp.complication", 
                            provider: Provider()) { entry in
            MyEntryView(entry: entry)
        }
        .configurationDisplayName(...)
        .description(...)
    }
}

Then use the same kind identifier in onContinueUserActivity like so:

.onContinueUserActivity("com.mycompany.myapp.complication", perform: { _ in
                            // Do something for complication
                        })