Using the share sheet to share content
In this post, we will explore how to use the share sheet in SwiftUI to share content from your app. The share sheet allows users to share text, images, URLs, and other types of content with other apps or services on their device.
What is ShareLink
?
ShareLink
is a view in SwiftUI that provides a way to share content using the system's share sheet. It allows you to specify the content you want to share, such as text, images, or URLs, and presents the share sheet when the user interacts with it.
Different ways to use ShareLink
ShareLink without title
You can create a ShareLink
without a title, which will use the default system-provided title for the share action.
ShareLink(
item: URL(string: "https://wesleydegroot.nl")!
)
ShareLink with customized link title
You can customize the title of the share link by providing a string as the first argument.
ShareLink(
"Share Website",
item: URL(string: "https://wesleydegroot.nl")!
)
ShareLink with a customized sharing subject and message
You can also customize the subject and message of the share action by providing Text
views for the subject
and message
parameters.
ShareLink(
item: URL(string: "https://wesleydegroot.nl")!,
subject: Text("Share blog"),
message: Text("Share blog post ShareLink")
)
ShareLink with a preview
You can provide a preview for the shared content using the preview
parameter. This allows you to specify a title and an icon for the preview.
ShareLink(
item: URL(string: "https://appsterdam.rs")!,
preview: SharePreview(
"Join Appsterdam!",
icon: Image(systemName: "person.3.sequence.fill")
)
)
ShareLink with a custom label
You can create a ShareLink
with a custom label by providing a closure that returns a view, such as a Label
.
ShareLink(item: URL(string: "https://wesleydegroot.nl")!) {
Label(
"Share my website",
systemImage: "star"
)
}
ShareLink with Transferable object
You can also share custom objects that conform to the Transferable
protocol. This allows you to share complex data types, such as images or structured data.
Example of a Transferable object
struct BlogPost: Transferable {
static var transferRepresentation: some TransferRepresentation {
ProxyRepresentation(exporting: \.image)
}
public var image: Image
public var title: String
public var description: String
}
Using ShareLink with a Transferable object
ShareLink(
item: post,
preview: SharePreview(post.title, image: post.image)
)
Full code example
import SwiftUI
struct ShareLinkDemoView: View {
let post: BlogPost = .init(
image: Image(systemName: "star"),
title: "Welcome to this blog post",
description: "With a description that is longer than the title"
)
var body: some View {
List {
Section("ShareLink without title") {
ShareLink(
item: URL(string: "https://wesleydegroot.nl")!
)
}
Section("ShareLink with customized link title") {
ShareLink(
"Share Website",
item: URL(string: "https://wesleydegroot.nl")!
)
}
Section("ShareLink with a customized sharing subject and message") {
ShareLink(
item: URL(string: "https://wesleydegroot.nl")!,
subject: Text("Share blog"),
message: Text("Share blog post ShareLink")
)
}
Section("ShareLink with a preview") {
ShareLink(
item: URL(string: "https://appsterdam.rs")!,
preview: SharePreview(
"Join Appsterdam!",
icon: Image(systemName: "person.3.sequence.fill")
)
)
}
Section("ShareLink with a custom label") {
ShareLink(item: URL(string: "https://wesleydegroot.nl")!) {
Label(
"Share my website",
systemImage: "star"
)
}
}
Section("ShareLink with Transferable object") {
ShareLink(
item: post,
preview: SharePreview(post.title, image: post.image)
)
}
}
}
}
struct BlogPost: Transferable {
static var transferRepresentation: some TransferRepresentation {
ProxyRepresentation(exporting: \.image)
}
public var image: Image
public var title: String
public var description: String
}
#Preview {
ShareLinkDemoView()
}
Screenshots
![]() |
![]() |
Conclusion
ShareLink is a powerful and flexible way to share content in SwiftUI applications. It allows you to easily present the system's share sheet and customize the content being shared. By using ShareLink, you can enhance the user experience of your app by enabling users to share content with their favorite apps and services seamlessly.
Read more
- What powers this website • 5 minutes reading time.
- Using MetricKit • 3 minutes reading time.
- Subscripts in Swift • 2 minutes reading time.
Share
Share Bluesky Mastodon Twitter LinkedIn Facebook