Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kyle-n/onpasteboardchange
SwiftUI View modifier that runs a callback on change to UIPasteboard or NSPasteboard
https://github.com/kyle-n/onpasteboardchange
appkit custom-pasteboards ios macos swiftui uikit
Last synced: 3 months ago
JSON representation
SwiftUI View modifier that runs a callback on change to UIPasteboard or NSPasteboard
- Host: GitHub
- URL: https://github.com/kyle-n/onpasteboardchange
- Owner: kyle-n
- License: mit
- Created: 2020-11-17T21:30:12.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-07T15:08:27.000Z (about 4 years ago)
- Last Synced: 2024-09-17T23:06:51.639Z (4 months ago)
- Topics: appkit, custom-pasteboards, ios, macos, swiftui, uikit
- Language: Swift
- Homepage: https://cocoapods.org/pods/OnPasteboardChange
- Size: 264 KB
- Stars: 19
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OnPasteboardChange
A SwiftUI View modifier that triggers a callback whenever the pasteboard changes. Works with UIKit and AppKit - perfect for cross-platform SwiftUI projects.
## Installation
Supports iOS 14.0+ and macOS 11.0+.
### Swift Package Manager
In Xcode, File -> Swift Packages -> Add Package Dependency... and paste `https://github.com/kyle-n/OnPasteboardChange`.
### CocoaPods
Add `pod OnPasteboardChange` to your `Podfile` and run `pod install` in your project directory.
## Usage
```swift
import OnPasteboardChangestruct ContentView: View {
@State private var text: String = ""
var body: some View {
VStack {
TextEditor(text: $text)
.padding()
.onPasteboardChange {
print("The clipboard was changed!")
let latestItem = UIPasteboard.general.items.first
}
}
}
}
```All this modifier does is alert your code the pasteboard has changed. Your code can then check the pasteboard, read from it, write to it or do anything you like. This way, your code can control when to check the clipboard (and when to trigger the iOS 14 "X app pasted from Y app" alert).
The modifier will pick up changes to the pasteboard while the app is in the background. This works on iOS and macOS.
### Custom pasteboards
By default, `.onPasteboardChange` will watch `UIPasteboard.general` or `NSPasteboard.general` for changes. However, if you have a custom pasteboard, you can track changes on that as well.
```swift
import OnPasteboardChangestruct ContentView: View {
@State private var text: String = ""
let pb = UIPasteboard.withUniqueName()
var body: some View {
VStack {
Button("Add to Custom Pasteboard") {
pb.addItems([["newItem": "value"]])
}
TextEditor(text: $text)
.padding()
.onPasteboardChange(for: pb) {
// only triggered after pressing "Add to Custom Pasteboard"
print("custom pb changed")
}
}
}
}
```## Credits
Created by [Kyle Nazario](https://twitter.com/kbn_au).