ShareLink for Swift: Complete Guide with Examples
ShareLink is a built-in structure in the SwiftUI framework designed for creating "Share" buttons in Swift apps. It provides a simple, intuitive interface for integrating the system share sheet (UIActivityViewController) without writing complex UIKit code. This component debuted in iOS 16, macOS 13, tvOS 16, and watchOS 9, and has since become the standard way to implement content sharing in modern SwiftUI apps.
Why use ShareLink? Today's users constantly share content: links, photos, text, and files. Manually implementing sharing requires creating a UIActivityViewController, configuring delegates, and handling various data types. ShareLink handles all this work, offering a ready-made SwiftUI component that automatically adapts to the platform and system settings. This not only speeds up development but also ensures a consistent user experience across all Apple devices.
This component is perfect for: social apps (sharing posts), news aggregators (sharing articles), photo editors (exporting images), messengers (sending files), and any other app needing a "Share" feature. ShareLink supports all standard data types: strings, URLs, images, files, and custom items conforming to the Transferable protocol.
Setup
ShareLink is part of the SwiftUI framework, so no separate installation via package managers is required. Simply import SwiftUI into your project:
import SwiftUI
Minimum requirements:
- iOS 16.0+ / macOS 13.0+ / tvOS 16.0+ / watchOS 9.0+
- Xcode 14.0+
- Swift 5.7+
If you're targeting older iOS versions, you'll need to use UIActivityViewController directly via UIViewRepresentable.
Quick Start
The simplest way to use ShareLink is to pass it a string or URL. Here's a minimal working example:
import SwiftUI
struct ContentView: View {
var body: some View {
ShareLink(item: URL(string: "https://pythonlib.ru")!) {
Label("Share Link", systemImage: "square.and.arrow.up")
}
.padding()
}
}
This code creates a button with the "Share" icon. Tapping it opens the system share sheet, where users can choose an app to send the link.
Key Methods and Classes
ShareLink offers several initializers and modifiers for customization. Here are 8 essential features.
1. ShareLink(item:)
Signature: init(item: T, subject: Text? = nil, message: Text? = nil, @ViewBuilder label: () -> Label)
What it does: Creates a share button for a single item. The item parameter can be a string, URL, image, or any type conforming to the Transferable protocol.
ShareLink(item: "Hello, world!") {
Text("Share Text")
}
2. ShareLink(items:)
Signature: init(items: [T], subject: Text? = nil, message: Text? = nil, @ViewBuilder label: () -> Label)
What it does: Allows sharing multiple items at once (e.g., an array of photos).
let images = [UIImage(named: "photo1")!, UIImage(named: "photo2")!]
ShareLink(items: images) {
Label("Share Photos", systemImage: "photo.on.rectangle")
}
3. ShareLink(item:preview:)
Signature: init(item: T, preview: SharePreview<T>, @ViewBuilder label: () -> Label)
What
Also in library
Working with Images in Go (Golang): Libraries and Examples
openai-api-rs for Rust: Complete Guide with Code Examples
Prometheus Metrics in Swift: Monitoring iOS/macOS Applications
Data Validation in C# (.NET) with FluentValidation: A Complete Guide