I want to get which button in AccessoryWidgetGroup was pressed in the Watch App, but I can't.
When I use Button(_:intent:), it works in Smart Stack, but it doesn't work in Complication Widget.
Using widgetURL(_:) always gets the URL of the first button.
Below is a snippet of the code I tried. All codes can be found here.
struct MyWatchWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
AccessoryWidgetGroup("Widget Group") {
MyWatchWidgetEntryButton(intent: .init(id: "001", imageName: "star.fill"))
MyWatchWidgetEntryButton(intent: .init(id: "002", imageName: "heart.fill"))
MyWatchWidgetEntryButton(intent: .init(id: "003", imageName: "leaf.fill"))
}
}
}
//Button View
private struct MyWatchWidgetEntryButton: View {
@Environment(\.showsWidgetContainerBackground) var showsWidgetContainerBackground
let intent: MyAppIntent
var body: some View {
Button(intent: intent) {
ZStack {
if showsWidgetContainerBackground {
Color.black
} else {
AccessoryWidgetBackground()
}
VStack {
Image(systemName: intent.imageName)
.font(.headline)
Text(intent.id)
.font(.system(size: 10, weight: .bold))
}
}
}
.buttonStyle(.plain)
.widgetURL(URL(string: "widget://" + intent.id))
}
}
Does anyone know how to do this?
Thank you.
I got a chance to try your code and found something interesting: When I tap a widget group element in the complication, the app intent is not really triggered. It seems that watchOS detects that the tap is on the whole widget group, and launches your watchOS app with the first widget URL.
Interestingly, if I remove the .buttonStyle(.plain)
view modifier, as shown below, watchOS will detect that the tap is on the widget group element, and triggers the associated app intent, which triggers the .performAppIntent
notification and passes your app the right data.
Button(intent: intent) {
...
}
//.buttonStyle(.plain)
.widgetURL(URL(string: "widget://" + intent.id))
So removing .buttonStyle(.plain)
may be the easiest way to fix your issue. If the plain style button doesn't fit your design, you can probably manage to make the widget group element bigger, which I believe should work as well.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.