Declaration
Initializers
12 SwiftUI initializers back <ShareLink>. The props you pass pick one.
<ShareLink
item: string
subject?: string | null
message?: string | null
/>nonisolated init(
item: URL,
subject: Text? = nil,
message: Text? = nil
)
where Data == CollectionOfOne<URL>item*stringThe item to share.
subjectstring | nullA title for the item to show when sharing to activities that support a subject field.
Default
`nil`messagestring | nullA description of the item to show when sharing to activities that support a message field. Activities may support attributed text or HTML strings.
Default
`nil`
Also takes the common view props: modifiers, style and testID.
Overview
People tap or click on a share link to present a share interface. The link typically uses a system-standard appearance; you only need to supply the content to share:
ShareLink(item: URL(string: "https://developer.apple.com/xcode/swiftui/")!)You can control the appearance of the link by providing view content.
For example, you can use a Label to display a
link with a custom icon:
ShareLink(item: URL(string: "https://developer.apple.com/xcode/swiftui/")!) {
Label("Share", image: "MyCustomShareIcon")
}If you only wish to customize the link's title, you can use one of the
convenience initializers that takes a string and creates a Label for you:
ShareLink("Share URL", item: URL(string: "https://developer.apple.com/xcode/swiftui/")!)The link can share any content that is doc://com.apple.documentation/documentation/coretransferable/transferable. Many framework types, like doc://com.apple.documentation/documentation/Foundation/URL, already conform to this protocol. You can also make your own types transferable.
For example, you can use doc://com.apple.documentation/documentation/coretransferable/proxyrepresentation to resolve your own type to a framework type:
struct Photo: Transferable {
static var transferRepresentation: some TransferRepresentation {
ProxyRepresentation(\.image)
}
public var image: Image
public var caption: String
}
struct PhotoView: View {
let photo: Photo
var body: some View {
photo.image
.toolbar {
ShareLink(
item: photo,
preview: SharePreview(
photo.caption,
image: photo.image))
}
}
}Sometimes the content that your app shares isn't immediately available. You can use doc://com.apple.documentation/documentation/coretransferable/filerepresentation or doc://com.apple.documentation/documentation/coretransferable/datarepresentation when you need an asynchronous operation, like a network request, to retrieve and prepare the content.
Note that some applications offer their sharing service for files,
but not for a wide range of different data types, for example, Mail.app,
Notes.app, Messages.app or AirDrop. If you don't see a particular
sharing service in the presented ShareLink, try adding a
doc://com.apple.documentation/documentation/coretransferable/filerepresentation
to the type's Transferable conformance.
A Transferable type also lets you provide multiple content types for a
single shareable item. The share interface shows relevant sharing services
based on the types that you provide.
The previous example also shows how you provide a preview of your content to show in the share interface.
A preview isn't required when sharing URLs or non-attributed strings. When sharing these types of content, the system can automatically determine a preview.
You can provide a preview even when it's optional. For instance, when sharing URLs, the automatic preview first shows a placeholder link icon alongside the base URL while fetching the link's metadata over the network. The preview updates once the link's icon and title become available. If you provide a preview instead, the preview appears immediately without fetching data over the network.
Some share activities support subject and message fields. You can
pre-populate these fields with the subject and message parameters:
ShareLink(
item: photo,
subject: Text("Cool Photo"),
message: Text("Check it out!"),
preview: SharePreview(
photo.caption,
image: photo.image))From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Navigation.
- LinkA control for navigating to a URL.
- NavigationLinkA view that controls a navigation presentation.
- NavigationSplitViewA view that presents views in two or three columns, where selections in leading columns control presentations in subsequent columns.
- NavigationStackA view that displays a root view and enables you to present additional views over the root view.
- TabThe content for a tab and the tab's associated tab item in a tab view.
- TabsTabbed navigation (SwiftUI TabView with Tab). Each Trigger declares a tab; the Content with the same value is its page.
- TabSectionA container that you can use to add hierarchy within a tab view.
- TabViewA view that switches between multiple child views using interactive user interface elements.