Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/swiftuiux/sharelink-for-swiftui-example
example demonstrates how to use ShareLink button for SwiftUI to share various types of data such as strings, URLs, images, data, attributed strings, and location ios14
https://github.com/swiftuiux/sharelink-for-swiftui-example
button ios14 sharelink swift swiftui
Last synced: about 2 months ago
JSON representation
example demonstrates how to use ShareLink button for SwiftUI to share various types of data such as strings, URLs, images, data, attributed strings, and location ios14
- Host: GitHub
- URL: https://github.com/swiftuiux/sharelink-for-swiftui-example
- Owner: swiftuiux
- Created: 2024-07-03T07:20:56.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-11-27T11:49:04.000Z (2 months ago)
- Last Synced: 2024-11-28T18:41:36.760Z (2 months ago)
- Topics: button, ios14, sharelink, swift, swiftui
- Language: Swift
- Homepage:
- Size: 4.52 MB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://github.com/swiftuiux/sharelink-swiftui-ios).
![ShareLinkButton Example](https://github.com/swiftuiux/sharelink-for-swiftui-example/blob/main/img/sharelink_swiftui.jpeg)
## 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
```swift
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
```swift
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
```swift
let sampleImage = UIImage(named: "vision")!
ShareLinkButton(item: sampleImage) {
ButtonLabel(image: "square.and.arrow.up", text: "Share Image", backgroundColor: .orange)
}
```#### Sharing Data
```swift
let sampleData = "Sample data".data(using: .utf8)!
ShareLinkButton(item: sampleData) {
ButtonLabel(image: "square.and.arrow.up", text: "Share Data", backgroundColor: .purple)
}
```#### Sharing an NSAttributedString
```swift
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
```swift
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.
```swift
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)
}
```