How it works
A Link is a control that navigates to a URL when a person activates it. Use it whenever your interface needs to send someone to a web page or another resource that a registered URL scheme can handle, rather than to a destination inside your own view hierarchy. Because it is presented as standard SwiftUI content, a Link adapts its appearance to its surroundings and lets the system open the URL with the appropriate handler — typically the default browser for an https address.
Create a text link with Link(_:destination:)
The string-based initializer pairs a visible title with the URL to open. It is the most direct way to present a tappable label, taking the displayed text and a
destinationURL. In the example,Link("Visit Apple", destination: URL(string: "https://www.apple.com")!)shows the text "Visit Apple" and opens Apple's home page when activated.Supply the destination as a URL
Every
Linkis driven by adestinationparameter of typeURL, which determines where activation leads and which handler the system invokes. You build it from a string withURL(string:), as inURL(string: "https://developer.apple.com")!; force-unwrapping is acceptable only for known-valid literals like these.Provide custom content with the trailing-closure initializer
When a plain title isn't enough, use
Link(destination:) { ... }, whoselabelclosure can return any view. This lets the link carry richer content while keeping its link behavior. The example passes aLabel("Developer Docs", systemImage: "book")so the control shows an icon alongside text yet still opens the developer documentation URL.Apply modifiers like any other view
A
Linkconforms toView, so you compose and style it with the same modifiers and containers you use elsewhere. Here both links sit inside aVStackand inherit the surrounding.padding(), demonstrating that aLinkplugs into ordinary layout without special handling.
URL(string: "mailto:support@example.com")! and watch activation open a mail composer instead of a web page, showing that Link dispatches to whatever handler the URL scheme designates.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct LinkDemo: View {
var body: some View {
VStack(alignment: .leading, spacing: 16) {
Link("Visit Apple", destination: URL(string: "https://www.apple.com")!)
Link(destination: URL(string: "https://developer.apple.com")!) {
Label("Developer Docs", systemImage: "book")
}
}
.padding()
}
}