WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

How to let the user share content with ShareLink

Paul Hudson    @twostraws   

SwiftUI's ShareLink view lets users export content from our app to share elsewhere, such as saving a picture to their photo library, sending a link to a friend using Messages, and more.

We provide the content we want to share, and iOS takes care of showing all the apps that can handle the data we're sending. For example, we can share a URL like this:

ShareLink(item: URL(string: "https://www.hackingwithswift.com")!)

That's going to make a button saying "Share" with an icon attached to it, and pressing it will bring up the iOS share sheet. If you're in the simulator you'll only see a few things there as samples, and some might not even work, but if you use a real device you'll see you can share that URL just fine.

If you want more control over the data, you have several options.

First, you can attach a subject and message to the shared data like this:

ShareLink(item: URL(string: "https://www.hackingwithswift.com")!, subject: Text("Learn Swift here"), message: Text("Check out the 100 Days of SwiftUI!"))

How that information is used depends on the app users share to – the URL will always be attached, because that's the most important thing, but some apps will use the subject, some the message, and others will use both.

Second, you can customize the button itself by providing whatever label you want:

ShareLink(item: URL(string: "https://www.hackingwithswift.com")!) {
    Label("Spread the word about Swift", systemImage: "swift")

And third, you can provide a preview to attach, which is important when you're sharing something more complex – it's possible to share entirely custom data here, so the preview is helpful for giving the recipient some idea of what's inside.

Annoyingly, this is needed even for data that is its own preview, such as an image. To avoid making your code repetitive, I'd suggest assigning the image to a local constant then using that:

let example = Image(.example)

ShareLink(item: example, preview: SharePreview("Singapore Airport", image: example)) {
    Label("Click to share", systemImage: "airplane")

Letting users share data from your app is really important, because without it your data can feel quite isolated from the rest of their life!

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.