0

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.

  1. Adding a handler for onTapGesture which never gets called
  2. Adding a onTapGesture for the parent HStack which never gets called
  3. Posting a notification from link's onOpenURL handler and receiving it in HStack's onReceive (refer How to set addObserver in SwiftUI?)
  4. Removed the Link and made it a Button with action: set, but handler never seems to get called

in vain. Any click on the widget dismisses it and opens the main app.

legends2k
  • 31,634
  • 25
  • 118
  • 222

0 Answers0