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.
Share a value with ShareLink(item:)
The simplest initializer takes a single
itemto share and supplies a default label and icon automatically. HereShareLink(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.Annotate the share with subject and message
Many initializers accept
subjectandmessageparameters 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")andmessage: Text("A great resource")travel alongside the sharedURL(string: "https://developer.apple.com")!.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
ShareLinkprovidesLabel("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.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 ownTransferablemodel when you need to export richer content.
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.
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()
}
}