是否可以使用 SwiftUI AttributedString 中的链接导航到同一应用程序中的另一个视图?

Mau*_*min 4 deep-linking swift swiftui swiftui-navigationstack

我知道使用 AttributedString,我可以使用以下内容链接到外部网站:

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

我可以使用它在同一个应用程序中从 ViewA 转到 ViewB,有点像 Twitter 中的这个示例吗?

推特

rob*_*off 7

OpenURLAction您可以在环境中存储来覆盖子视图喜欢TextLink打开 URL 的方式。

这个例子来自文档:

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
    })
Run Code Online (Sandbox Code Playgroud)

environment您可能希望在视图层次结构的较高级别(靠近根视图)应用修改器,而不是直接在每个Text视图上应用。