swiftuiapple-watch-complicationclockkit

What is the ActivityType when handling the launch of a complication with onContinueUserActivity in swiftUI


I have a SwiftUI Apple Watch app. When a user taps on a complication, I want to show the appropriate view. I think I can do so with an onContinueUserActivity modifier but I can't find the appropriate ActivityType.

I named it CLKComplicationTapActivityType below but I need the real constant.

struct ContentView: View {

    var body: some View {
        StationList()
            .onContinueUserActivity(CLKComplicationTapActivityType, perform: handleComplicationTap)
    }
    
    func handleComplicationTap(_ userAcivity: NSUserActivity){
        if let date = userAcivity.userInfo?[CLKLaunchedTimelineEntryDateKey]{
            \\ handle the tap
        }
    }
}



Solution

  • See https://developer.apple.com/documentation/clockkit/clkcomplicationdescriptor/3612140-useractivity

    When the user taps on a complication specified by this configuration, the system launches the app and calls handle(_:), passing the user activity.

    You can define your own custom activity:

    let YourActivity = NSUserActivity(activityType: "com.foo.customconstant")
    

    And then assign it to the userActivity property of the CLKComplicationDescriptor that you return in CLKComplicationDataSource.getComplicationDescriptors(handler:).