TechnologiesSwiftUI

Link struct

iOSmacOStvOSwatchOSvisionOSiOS 14.0+✓ renders

A control for navigating to a URL.

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.

  1. 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 destination URL. 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.

  2. Supply the destination as a URL

    Every Link is driven by a destination parameter of type URL, which determines where activation leads and which handler the system invokes. You build it from a string with URL(string:), as in URL(string: "https://developer.apple.com")!; force-unwrapping is acceptable only for known-valid literals like these.

  3. Provide custom content with the trailing-closure initializer

    When a plain title isn't enough, use Link(destination:) { ... }, whose label closure can return any view. This lets the link carry richer content while keeping its link behavior. The example passes a Label("Developer Docs", systemImage: "book") so the control shows an icon alongside text yet still opens the developer documentation URL.

  4. Apply modifiers like any other view

    A Link conforms to View, so you compose and style it with the same modifiers and containers you use elsewhere. Here both links sit inside a VStack and inherit the surrounding .padding(), demonstrating that a Link plugs into ordinary layout without special handling.

Try it — Change the first link's destination to 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.

Link.swift
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()
    }
}
Live preview
Visit Apple Developer Docs
Visit Apple Developer Docs
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →