BLACK FRIDAY: Save 50% on all my books and bundles! >>

How to open web links in Safari

Paul Hudson    @twostraws   

Updated for Xcode 16.4

Improved in iOS 26

SwiftUI gives us several ways of linking to web content, including a dedicated Link view that looks like a button but opens a URL in Safari when pressed, and an openURL action we can call programmatically.

To use Link just give it a title for the button, plus a destination URL to show, like this:

Link("Learn SwiftUI", destination: URL(string: "https://www.hackingwithswift.com/quick-start/swiftui")!)

Download this as an Xcode project

As it’s just a text link, you can customize it with a font, color, and more:

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

Download this as an Xcode project

The words “Visit Apple” in red.

And if you’d rather create your own view rather than just use text, you can do that too:

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

Download this as an Xcode project

A link icon on a blue circle.

Alternatively, you can open a URL from code by using the openURL environment key. In its simplest form this will open your link in Safari, like this:

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

    var body: some View {
        Button("Visit Apple") {
            openURL(URL(string: "https://www.apple.com")!)
        }
    }
}

Download this as an Xcode project

A “Visit Apple” link in blue.

However you can also load URLs in an in-app browser that's equivalent to SFSafariViewController for UIKit developers. To use that instead, add prefersInApp: true to your openURL() call, like this:

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

    var body: some View {
        Button("Visit Apple") {
            openURL(URL(string: "https://www.apple.com")!, prefersInApp: true)
        }
    }
}

Download this as an Xcode project

Save 50% in my Black Friday sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further for less! Get my all-new book Everything but the Code to make more money with apps, get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn Swift Testing, design patterns, and more.

Save 50% on all our books and bundles!

Similar solutions…

BUY OUR BOOKS
Buy Everything but the Code Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Interview Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.0/5

 
Unknown user

You are not logged in

Log in or create account