Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active July 12, 2020 15:45
Show Gist options
  • Save sturdysturge/4106de7aa57b7b0d4f4b5b06f27c3634 to your computer and use it in GitHub Desktop.
Save sturdysturge/4106de7aa57b7b0d4f4b5b06f27c3634 to your computer and use it in GitHub Desktop.
RevDoc PasteButton
import SwiftUI
import UniformTypeIdentifiers
@available(OSX 10.16, *)
@available(iOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
struct PasteButtonView: View {
@State var text = String()
let utType = UTType.text
//The declaration above is equivalent to
//let utType = UTType("public.utf8-plain-text")!
var body: some View {
VStack {
Text(text)
PasteButton(supportedContentTypes: [utType], payloadAction: { array in
guard let firstItem = array.first else {
return
}
firstItem.loadDataRepresentation(forTypeIdentifier: "public.utf8-plain-text", completionHandler: {
(data, error) in
guard let data = data else {
return
}
let loadedText = String(decoding: data, as: UTF8.self)
self.text = loadedText
//This call just shows how to find print the UTI type of any type conforming to NSItemProviderWriting, "public.utf8-plain-text" in this case
self.getUTITypeString(for: loadedText)
})
})
}
.frame(width: 200, height: 200)
}
func getUTITypeString(for item: Any) {
if let item = item as? NSItemProviderWriting {
let provider = NSItemProvider(object: item)
print(provider)
}
else {
print("This data type cannot be used in an NSItemProvider")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment