Tap action for new complications of watchOS 9

344 Views Asked by At

In earlier watchOS versions (ver>9) we can use complication descriptor to add complications and metadata like userActivity for handle tap action in the watch app delegate (according to AppleDocs):

If you create a complication descriptor using the init(identifier:displayName:supportedFamilies:userActivity:) initializer to pass in an NSUserActivity object, when the user taps the corresponding complication, the system activates your app and passes the activity object to your onContinueUserActivity(_:perform:) modifiers.

But this "CLKComplicationDescriptor(identifier:displayName:supportedFamilies:userActivity:)" is using ClockKit and is deprecated now. I can't find any replacement for handling the tap event on new complications. I'm using watchOS 9 with Swift-UI. I want to do an action in my watch app when the user taps on my .accessoryCorner complication. I'm handling this behavior in my iOS app and widgets using .widgetURL(myUrl) and handling it in my SceneDelegate, but it does not work in the watch.

2

There are 2 best solutions below

0
On

Use the .widgetURL modifier in your action widget. When tapped, this will launch your watch app and the onOpenURL() method receives the url included in the .widgetURL

2
On
func handle(_ userActivity: NSUserActivity) {
    debugPrint("userActivity: \(userActivity.activityType)")
    guard let identifier = (userActivity.interaction?.intent as? ConfigurationIntent)?.style, let type = ComplicationType(rawValue: identifier) else {
        return
    }
    debugPrint("identifier: \(identifier)")
}

if you use intent in widgetkit, use can identifier the style from custom intent(ConfigurationIntent), then you can check out which widget is clicked