-
-
Save jnadeau/3fb7fb34207eaa79da0bdd7cdcce5089 to your computer and use it in GitHub Desktop.
Handling input from services like "Insert From iPhone"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Insert from Services | |
extension ViewController: NSServicesMenuRequestor { | |
// For the sake of this example, this responder doesn't know how to send data to services (e.g. services that accept images or text), but it does know how to accept images. We check if the returned pasteboard type is something NSImage can handle. | |
override func validRequestor(forSendType sendType: NSPasteboard.PasteboardType?, | |
returnType: NSPasteboard.PasteboardType?) -> Any? { | |
if let typeToInsert = returnType, NSImage.imageTypes.contains(typeToInsert.rawValue), sendType == nil { | |
return self | |
} | |
return super.validRequestor(forSendType: sendType, returnType: returnType) | |
} | |
// If we are handed an image by a service, stuff it in our NSCollectionView. | |
func readSelection(from pboard: NSPasteboard) -> Bool { | |
if let image = NSImage(pasteboard: pboard) { | |
appendImageToCollection(image) | |
return true | |
} | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment