In iOS 16, SwiftUI comes with a brand new view known as ShareLink
. When customers faucet on the share hyperlink, it presents a share sheet for customers to share content material to different functions or copy the information for later use.
The ShareLink
view is designed to share any forms of knowledge. On this tutorial, we’ll present you how you can use ShareLink
to let customers share textual content, URL, and pictures.
Primary Utilization of ShareLink
Let’s start with an instance. To create a share hyperlink for sharing a URL, you possibly can write the code like this:
var physique: some View {
VStack {
ShareLink(merchandise: url)
}
}
}
struct ContentView: View { Â Â Â Â non-public let url = URL(string: “https://www.appcoda.com”)! Â Â Â Â Â var physique: some View { Â Â Â Â Â Â Â Â VStack { Â Â Â Â Â Â Â Â Â Â Â Â ShareLink(merchandise: url) Â Â Â Â Â Â Â Â } Â Â Â Â } } |
SwiftUI mechanically renders a Share button with a small icon.

When tapped, iOS brings up a share sheet for customers to carry out additional actions corresponding to copy and including the hyperlink to Reminders.

To share textual content, as an alternative of URL, you possibly can merely go the a string to the merchandise
parameter.
ShareLink(merchandise: “Take a look at this new characteristic on iOS 16”) |
Customizing the Look of Share Hyperlink
To customise the looks of the hyperlink, you possibly can present the view content material within the closure like this:
ShareLink(merchandise: url) { Â Â Â Â Picture(systemName: “sq..and.arrow.up”) } |
On this case, SwiftUI solely shows the share icon for the hyperlink.

Alternatively, you possibly can current a label with a system picture or customized picture:
ShareLink(merchandise: url) { Â Â Â Â Label(“Faucet me to share”, systemImage:Â Â “sq..and.arrow.up”) } |
When initializing the ShareLink
occasion, you possibly can embody two extra parameters to offer further details about the shared merchandise:
ShareLink(merchandise: url, topic: Textual content(“Take a look at this hyperlink”), message: Textual content(“If you wish to be taught Swift, check out this web site.”)) { Â Â Â Â Picture(systemName: “sq..and.arrow.up”) } |
The topic
parameter permits you to embody a title in regards to the URL or any merchandise to share. The message
parameter means that you can specify the outline of the merchandise. Relying on the actions the consumer shares to, iOS will current the topic or message or each. Say, if you happen to add the URL to Reminders, the Reminders app shows the preset message.

Sharing Pictures
Aside from URLs, you possibly can share photographs utilizing ShareLink
. Here’s a pattern code snippet:
var physique: some View {
VStack(alignment: .main, spacing: 10) {
photograph
.resizable()
.scaledToFit()
ShareLink(merchandise: photograph, preview: SharePreview(“Huge Ben”, picture: photograph))
}
.padding(.horizontal)
}
}
struct ContentView: View {     non-public let photograph = Picture(“bigben”)      var physique: some View {         VStack(alignment: .main, spacing: 10) {             photograph                 .resizable()                 .scaledToFit()              ShareLink(merchandise: photograph, preview: SharePreview(“Huge Ben”, picture: photograph))          }         .padding(.horizontal)     } } |
For the merchandise
parameter, you specify the picture to share. And, you present a preview of the picture by passing an occasion of SharePreview
. Within the preview, you specify the title of the picture and the thumbnail. Whenever you faucet the Share button, iOS brings up a share sheet with the picture preview.

Conforming to Transferable
Aside from URLs, the merchandise
parameter accepts any objects that conforms to the Transferable
protocol. In iOS, the next sorts are the usual Transferable
sorts:
- String
- Information
- URL
- Attributed String
- Picture
Transferable is a protocol that describes how a kind interacts with transport APIs corresponding to drag and drop or copy and paste.
So what when you have a customized object, how will you make it transferable? Let’s say, you create the next Picture
construction:
struct Picture: Identifiable {     var id = UUID()     var picture: Picture     var caption: String     var description: String } |
To let ShareLink
share this object, it’s a must to undertake the Transferable
protocol for Picture
and implement the transferRepresentation
property:
extension Picture: Transferable { Â Â Â Â static var transferRepresentation: some TransferRepresentation { Â Â Â Â Â Â Â Â ProxyRepresentation(exporting: .picture) Â Â Â Â } } |
There are a variety of Switch Representations together with ProxyRepresentation
, CodableRepresentation
, DataRepresentation
, and FileRepresentation
. Within the code above, we use the ProxyRepresentation
which is a switch illustration that makes use of one other kind’s switch illustration as its personal. Right here, we use the Picture
‘s built-in Transferable
conformance.

Since Picture
now conforms to Transferable
, you possibly can go the Picture
occasion to ShareLink
:
ShareLink(merchandise: photograph, preview: SharePreview(photograph.caption, picture: photograph.picture)) |
When customers faucet the Share button, the app brings up the share sheet for sharing the photograph.
What’s Subsequent
This tutorial reveals you how you can use ShareLink
for sharing textual content, URL, and pictures. Actually, this new view means that you can share any forms of knowledge so long as the kind conforms to the Transferable
protocol.
For customized sorts, you undertake the protocol and supply a switch illustration through the use of one of many built-in TransferRepresentation
sorts. We briefly talk about the ProxyRepresentation
kind. If you might want to share a file between functions, you should use the FileRepresentation
kind. In later tutorials, we’ll talk about extra on that.