SwiftUI in iOS 16: Using ShareLink for Sharing Content Like Text and Photos
In iOS 16, SwiftUI comes with a new view called ShareLink
. When users tap on the share link, it presents a share sheet for users to share content to other applications or copy the data for later use.
The ShareLink
view is designed to share any types of data. In this tutorial, we will show you how to use ShareLink
to let users share text, URL, and images.
Basic Usage of ShareLink
Let’s begin with an example. To create a share link for sharing a URL, you can write the code like this:
struct ContentView: View {
private let url = URL(string: "https://www.appcoda.com")!
var body: some View {
VStack {
ShareLink(item: url)
}
}
}
SwiftUI automatically renders a Share button with a small icon.
When tapped, iOS brings up a share sheet for users to perform further actions such as copy and adding the link to Reminders.
To share text, instead of URL, you can simply pass the a string to the item
parameter.
ShareLink(item: "Check out…