I've a macOS SwiftUI widget (using WidgetKit) with minimum supported OS version set to 11. This detail is important as I want to run some code when a link view is clicked/tapped by the user:
Link("StackOverflow.com", destination: URL(string: "https://www.stackoverflow.com/")!)
.environment(\.openURL, OpenURLAction { url in
print("hello, world")
return .systemAction
}
)
SwiftUI: use Link with onTapGesture asserts that this only works on macOS 12+ as it's a read-only property in macOS 11. However, irrespective of the OS version, this doesn't work on widgets; it works only on apps.
- Adding a handler for
onTapGesture
which never gets called - Adding a
onTapGesture
for the parentHStack
which never gets called - Posting a notification from link's
onOpenURL
handler and receiving it in HStack'sonReceive
(refer How to set addObserver in SwiftUI?) - Removed the
Link
and made it aButton
withaction:
set, but handler never seems to get called
in vain. Any click on the widget dismisses it and opens the main app.