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.
Provide a title with the SharePreview initializer
The first argument to
SharePreviewis the title text shown for the shared item in the share sheet. It accepts a string (or anyLocalizedStringKey/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 bareurl.Attach a thumbnail with the image parameter
The
image:parameter takes anImagethat 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 suppliesimage: Image(systemName: "swift")so the SF Symbol becomes the preview's picture.Hand the preview to ShareLink
A
SharePreviewis not presented on its own—it is passed toShareLinkvia itspreview:parameter, which pairs the preview with theitembeing shared. SwiftUI then uses yourSharePreviewto populate the sheet thatShareLinkpresents. In the example theSharePreviewdescribes how theurlshared byShareLink(item: url, preview:)is displayed.Match the preview to the shared item
Because the preview only controls presentation, the title and image you give
SharePreviewshould describe whateveritemtheShareLinkactually shares. Keeping them aligned ensures the sheet's preview is an accurate, recognizable summary of the content. Here the swift.orgurlis represented by a Swift-themed title and theswiftsymbol so the preview reads as that page.
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.
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()
}
}