2 Star 23 Fork 10

小弟调调/swiftui-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
README.md 1.11 KB
一键复制 编辑 原始数据 按行查看 历史

如何在 Safari 中打开 Web 链接?

SwiftUI为我们提供了专用的 Link 视图,该视图看起来像一个按钮,但是在按下时会在 Safari 中打开一个 URL。 它非常易于使用-只需为其按钮标题加上要显示的目标 URL,如下所示:

Link("Learn SwiftUI", destination: URL(string: "https://www.apple.com")!)

由于它只是文本链接,因此可以使用字体,颜色等进行自定义:

Link("Visit Apple", destination: URL(string: "https://www.apple.com")!)
    .font(.title)
    .foregroundColor(.red)

而且,如果您想创建自己的视图而不是仅使用文本,也可以这样做:

Link(destination: URL(string: "https://www.apple.com")!) {
    Image(systemName: "link.circle.fill")
        .font(.largeTitle)
}

或者,您可以使用 openURL 环境键从代码中打开 URL,如下所示:

struct ContentView: View {
    @Environment(\.openURL) var openURL

    var body: some View {
        Button("Visit Apple") {
            openURL(URL(string: "https://www.apple.com")!)
        }
    }
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Swift
1
https://gitee.com/jaywcjlove/swiftui-example.git
git@gitee.com:jaywcjlove/swiftui-example.git
jaywcjlove
swiftui-example
swiftui-example
main

搜索帮助