单击媒体图标时如何 link 社交媒体 SwiftUI

How to link social media when clicked media icon SwiftUI

我正在尝试在用户单击社交媒体图标时创建 link。我怎样才能用 SwiftUI 做到这一点。我找不到任何来源。

有两种方法可以做到:

第一个:

struct ContentView: View {
    var body: some View {
        Link(destination: URL(string: "")!) // <- Add your link here
        {
            Image(systemName: "link.circle.fill") // <- Change icon to your preferred one
                .font(.largeTitle)
        }
    }
}

第二个:

struct ContentView: View {
    @Environment(\.openURL) var openURL
    var body: some View {
        Button(action: {
            openURL(URL(string: "")!) // <- Add your link here
        }, label: {
           Image(systemName: "link.circle.fill") // <- Change icon to your preferred one 
            .resizable()
            .frame(width: 50, height: 50)
            .foregroundColor(.blue)
        })
    }
}