Skip to main content

How to let users share content using the system share sheet

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to let users share content using the system share sheet 관련

SwiftUI by Example

Back to Home

How to let users share content using the system share sheet | SwiftUI by Example

How to let users share content using the system share sheet

Updated for Xcode 15

New in iOS 16

SwiftUI's ShareLink view makes it easy to share any kind of data from your app, as long as it conforms to the Transferable protocol.

By default you get a simple “Share” label with the appropriate icon, but you can also provide your own title text, or an entirely custom label:

let link = URL(string: "https://hackingwithswift.com")!


VStack(spacing: 20) {
    ShareLink(item: link)
    ShareLink("Learn Swift here", item: link)
    ShareLink(item: link) {
        Label("Learn Swift here", systemImage: "swift")
    }
}

Download this as an Xcode projectopen in new window

If you want to attach some extra text to the content you're sharing, there's a message parameter to do just that:

let link = URL(string: "https://hackingwithswift.com")!

ShareLink(item: link, message: Text("Learn Swift here!"))

Download this as an Xcode projectopen in new window

For simple URLs the system is able to generate a preview on our behalf, but you can also provide a custom preview with some text and an image of your choosing, like this:

let link = URL(string: "https://www.admin.ch")!

VStack {
    ShareLink(
        item: link,
        preview: SharePreview(
            "Switzerland's flag: it's a big plus.",
            image: Image(systemName: "plus")
        )
    )
}

Download this as an Xcode projectopen in new window

Similar solutions…
How to display a bottom sheet | SwiftUI by Example

How to display a bottom sheet
How to prevent a sheet from being dismissed with a swipe | SwiftUI by Example

How to prevent a sheet from being dismissed with a swipe
How to let users import videos using PhotosPicker | SwiftUI by Example

How to let users import videos using PhotosPicker
How to show an action sheet | SwiftUI by Example

How to show an action sheet
How to let users select pictures using PhotosPicker | SwiftUI by Example

How to let users select pictures using PhotosPicker

이찬희 (MarkiiimarK)
Never Stop Learning.