TechnologiesSwiftUI

SharePreview struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A representation of a type to display in a share preview.

How it works

A SharePreview describes how an item appears in the system share sheet, giving you control over the title, image, and icon that represent the content being shared. SwiftUI can often derive a preview automatically for items that conform to Transferable, but for custom values—or whenever you want the presented title and thumbnail to differ from the defaults—you supply a SharePreview explicitly. You pass it to a ShareLink through the preview parameter so the sheet shows a meaningful representation before the user picks a destination.

  1. Provide a title with the SharePreview initializer

    The first argument to SharePreview is the title text shown for the shared item in the share sheet. It accepts a string (or any LocalizedStringKey/Text-compatible value), letting you present a human-readable name rather than the raw data. Here the preview is titled "The Swift Programming Language" instead of relying on the bare url.

  2. Attach a thumbnail with the image parameter

    The image: parameter takes an Image that the share sheet renders as the preview thumbnail for the content. This is what the user sees as the visual stand-in for the item across the sheet's destinations. The example supplies image: Image(systemName: "swift") so the SF Symbol becomes the preview's picture.

  3. Hand the preview to ShareLink

    A SharePreview is not presented on its own—it is passed to ShareLink via its preview: parameter, which pairs the preview with the item being shared. SwiftUI then uses your SharePreview to populate the sheet that ShareLink presents. In the example the SharePreview describes how the url shared by ShareLink(item: url, preview:) is displayed.

  4. Match the preview to the shared item

    Because the preview only controls presentation, the title and image you give SharePreview should describe whatever item the ShareLink actually shares. Keeping them aligned ensures the sheet's preview is an accurate, recognizable summary of the content. Here the swift.org url is represented by a Swift-themed title and the swift symbol so the preview reads as that page.

Try it — Change image: Image(systemName: "swift") to image: Image(systemName: "link") and open the share sheet to watch the preview thumbnail update to the new symbol.

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.

SharePreview.swift
struct SharePreviewDemo: View {
    let url = URL(string: "https://swift.org")!
    var body: some View {
        ShareLink(
            item: url,
            preview: SharePreview(
                "The Swift Programming Language",
                image: Image(systemName: "swift")
            )
        ) {
            Label("Share Swift.org", systemImage: "square.and.arrow.up")
        }
        .padding()
    }
}
Live preview
Share Swift.org
Share Swift.org
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →