Search code examples
swiftswiftuideep-linking

How to link social media when clicked media icon SwiftUI


Im trying to make a link when user click social media icons . How can I do that with SwiftUI. I couldn't find any source for that .

enter image description here


Solution

  • There are 2 ways to do it:

    First:

    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)
            }
        }
    }
    

    Second:

    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)
            })
        }
    }