Skip to content

Instantly share code, notes, and snippets.

@jnadeau
Created June 15, 2018 23:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnadeau/3fb7fb34207eaa79da0bdd7cdcce5089 to your computer and use it in GitHub Desktop.
Save jnadeau/3fb7fb34207eaa79da0bdd7cdcce5089 to your computer and use it in GitHub Desktop.
Handling input from services like "Insert From iPhone"
// 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