4

I know with AttributedString, I can link to an external website using something like:

Text("To learn more about AttributedString visit [this page](https://developer.apple.com/documentation/foundation/attributedstring/)")

Can I use this to go from ViewA to ViewB within the same app, kind of like this example in Twitter?

Twitter

M Alamin
  • 307
  • 2
  • 10
  • Does this answer your question? [Listen to link click in SwiftUI TextView](https://stackoverflow.com/questions/75192976/listen-to-link-click-in-swiftui-textview) – lorem ipsum Feb 12 '23 at 03:31

1 Answers1

6

You can store an OpenURLAction in the environment to override how subviews like Text and Link open URLs.

This example is from the documentation:

Text("Visit [Example Company](https://www.example.com) for details.")
    .environment(\.openURL, OpenURLAction { url in
        handleURL(url) // Define this method to take appropriate action.
        return .handled
    })

You may want to apply the environment modifier at a high level of your view hierarchy (near your root view) rather than directly on each Text view.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848