TechnologiesSwiftUI

ShareLink struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A view that controls a sharing presentation.

How it works

ShareLink is a control that presents the system share sheet, giving people a familiar way to send content from your app to other apps, contacts, and services. Rather than wiring up your own button and managing an activity-presentation flow, you describe the data you want to share and SwiftUI renders a standard share affordance and handles the presentation for you. Reach for ShareLink wherever a piece of content — a URL, a string, an image, or any custom transferable value — should be exportable from the current context.

  1. Share a value with ShareLink(item:)

    The simplest initializer takes a single item to share and supplies a default label and icon automatically. Here ShareLink(item: URL(string: "https://swift.org")!) produces a ready-made share control for that URL, so tapping it opens the share sheet preloaded with the link.

  2. Annotate the share with subject and message

    Many initializers accept subject and message parameters that hint how receiving apps should present the content — for example, a subject line in Mail or accompanying text in Messages. In the example, subject: Text("Check this out") and message: Text("A great resource") travel alongside the shared URL(string: "https://developer.apple.com")!.

  3. Customize the appearance with a label closure

    When the default presentation isn't right, a trailing closure lets you supply your own label view in place of the standard text and icon. The second ShareLink provides Label("Share Link", systemImage: "square.and.arrow.up"), replacing the default with a custom title and the conventional share glyph while the sharing behavior stays the same.

  4. Choose what kind of content to share

    ShareLink shares any value its initializers accept, including transferable types beyond URLs. Both controls here share a URL, but the same API surface lets you pass strings, images, or your own Transferable model when you need to export richer content.

Try it — Change item: URL(string: "https://swift.org")! to a plain string such as item: "Hello from SwiftUI" to see ShareLink offer text-sharing destinations instead of link-sharing ones.

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.

ShareLink.swift
struct ShareLinkDemo: View {
    var body: some View {
        VStack(spacing: 16) {
            ShareLink(item: URL(string: "https://swift.org")!)

            ShareLink(
                item: URL(string: "https://developer.apple.com")!,
                subject: Text("Check this out"),
                message: Text("A great resource")
            ) {
                Label("Share Link", systemImage: "square.and.arrow.up")
            }
        }
        .padding()
    }
}
Live preview
Share Share Link
Share Share Link
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →