Using an AttributedString link to open a view

I want to use a SwiftUI TextField with an AttributedString where if you click on a link within the AttributedString it opens another view within the same app and passes parameters to it.

E.g. you are within a text adventure. The AttributedString is "You stand at a crossroads. You can walk [north] or [south]", where the words north and south are each styled as a link and clicking on them opens another view that gets passed the parameter of which place you have chosen.

What is the "correct" way to do this? I imagine I could define lots of CustomURLs that do this but that seems like a hacky approach as it would be exposed outside the app. I really just want to link back and forth between views within my app.

Please file a feedback with this request.

A feedback asking for SwiftUI API to handle this case would be useful! In the meanwhile, you can embed a UITextField or UITextView that contains the same AttributedString (using the NSAttributedString(_:) conversion initializer), and set a delegate to handle tapped links, or construct a SwiftUI view that composes multiple Text instances in line and applies appropriate gesture handling.

Thank you, I will add this feedback. I was very briefly excited that I might leave NSAttributedString behind and write pure Swift/SwiftUI code but it seems that is not yet meant to be. Which honestly surprises me, it seems that if you introduce hyperlinks to text then the most obvious thing someone would want to do was to link to something within the app itself.

I also need this Feature! We just need a way to make a "OnTap" event.

You can achieve that with deep-linking: https://www.avanderlee.com/swiftui/deeplink-url-handling/

Using an AttributedString link to open a view
 
 
Q