This example demonstrates how to use ShareLinkButton in SwiftUI to share various types of data such as strings, URLs, images, data, attributed strings, and locations
- iOS 14.0+
- Swift 5.3+
- Xcode 12.0+
This example demonstrates how to use the functionality provided by the package.
The ShareLinkButton supports sharing the following types:
StringURLUIImageDataNSAttributedStringCLLocationUIActivityItemSource
Here are examples of how to use ShareLinkButton with various types of data.
let sampleString = "This is a sample string to share." ShareLinkButton(item: sampleString) { ButtonLabel(image: "square.and.arrow.up", text: "Share String", backgroundColor: .blue) }let sampleURL = URL(string: "https://www.apple.com")! ShareLinkButton(item: sampleURL) { ButtonLabel(image: "square.and.arrow.up", text: "Share URL", backgroundColor: .green) }let sampleImage = UIImage(named: "vision")! ShareLinkButton(item: sampleImage) { ButtonLabel(image: "square.and.arrow.up", text: "Share Image", backgroundColor: .orange) }let sampleData = "Sample data".data(using: .utf8)! ShareLinkButton(item: sampleData) { ButtonLabel(image: "square.and.arrow.up", text: "Share Data", backgroundColor: .purple) }let sampleAttributedString = NSAttributedString(string: "Simple sample NSAttributedString", attributes: [ .font: UIFont.systemFont(ofSize: 24), .foregroundColor: UIColor.red ]) ShareLinkButton(item: sampleAttributedString) { ButtonLabel(image: "square.and.arrow.up", text: "Share NSAttributedString", backgroundColor: .red) }let sampleLocation = CLLocation(latitude: 37.7749, longitude: -122.4194) ShareLinkButton(item: sampleLocation) { ButtonLabel(image: "square.and.arrow.up", text: "Share Location", backgroundColor: .yellow, textColor: .blue) }You can also create your own custom activity item sources.
class CustomActivityItemSource: NSObject, UIActivityItemSource { private let text: String init(text: String) { self.text = text } func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any { return text } func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? { return text } } ShareLinkButton(itemSource: CustomActivityItemSource(text: "Hello, world!")) { ButtonLabel(image: "square.and.arrow.up", text: "Share Custom Text", backgroundColor: .blue) }