Skip to content

Latest commit

 

History

History
115 lines (85 loc) · 3.05 KB

README.md

File metadata and controls

115 lines (85 loc) · 3.05 KB

ShareLink for SwiftUI Example

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

Requirements

  • iOS 14.0+
  • Swift 5.3+
  • Xcode 12.0+

Example Usage

This example demonstrates how to use the functionality provided by the package.

ShareLinkButton Example

Usage

Supported Types

The ShareLinkButton supports sharing the following types:

  • String
  • URL
  • UIImage
  • Data
  • NSAttributedString
  • CLLocation
  • UIActivityItemSource

Examples

Here are examples of how to use ShareLinkButton with various types of data.

Sharing a String

let sampleString = "This is a sample string to share."
ShareLinkButton(item: sampleString) {
    ButtonLabel(image: "square.and.arrow.up", text: "Share String", backgroundColor: .blue)
}

Sharing a URL

let sampleURL = URL(string: "https://www.apple.com")!
ShareLinkButton(item: sampleURL) {
    ButtonLabel(image: "square.and.arrow.up", text: "Share URL", backgroundColor: .green)
}

Sharing an Image

let sampleImage = UIImage(named: "vision")!
ShareLinkButton(item: sampleImage) {
    ButtonLabel(image: "square.and.arrow.up", text: "Share Image", backgroundColor: .orange)
}

Sharing Data

let sampleData = "Sample data".data(using: .utf8)!
ShareLinkButton(item: sampleData) {
    ButtonLabel(image: "square.and.arrow.up", text: "Share Data", backgroundColor: .purple)
}

Sharing an NSAttributedString

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)
}

Sharing a Location

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)
}

Creating a Custom Activity Item Source

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)
}