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

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

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

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% 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.