3

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


Ruben
  • 121
  • 1
  • 10
  • 1
    Check if you added activity type into Info.plist and try to change order of modifiers (1st - register, 2nd - handle). – Asperi Dec 14 '20 at 04:46
  • Did both already, sadly still not working. – Ruben Dec 14 '20 at 20:03
  • Did you managed to make it work? – Silviu St Jan 21 '21 at 18:35
  • @silviu, No. I think it’s a bug, because if you download the samples from the Apple website, it doesn’t work either. – Ruben Jan 22 '21 at 19:06
  • Hi @Ruben, I also followed the tutorial you mentioned and ran into the same problem. Then after looking at [Restoring Your App’s State with SwiftUI | Apple Developer Documentation](https://developer.apple.com/documentation/swiftui/restoring_your_app_s_state_with_swiftui), I learnt that adding ` activity.targetContentIdentifier = "some_id"` is required under Mac. Also, iCloud Key/Value capability is needed when sharing from Mac. – nkalvi Jul 06 '23 at 06:07

2 Answers2

0

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
                        })
Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53
-1

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.

MartianMartian
  • 1,753
  • 1
  • 18
  • 26