UIPasteboard: pasting images from Safari

I'm currently working on an app that has a chat feature. As part of this feature, I want to enable pasting images from the clipboard in order to send them as a message.

I overrode canPerformAction to allow pasting when the pasteboard has any images.

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if UIPasteboard.general.hasImages {
        return true
    } else {
        return super.canPerformAction(action, withSender: sender)
    }
}

Then I overrode paste method to get the image from UIPasteboard and send the message.

override func paste(_ sender: Any?) {
    if let image = UIPasteboard.general.image {
        if let data = UIPasteboard.general.data(forPasteboardType: kUTTypeGIF as String) {
            sendImageMessage(data, imageName: "Clipboard image.\(image.fileType())")
        } else if let data = image.jpegData(compressionQuality: 0.9) {
            sendImageMessage(data, imageName: "Clipboard image.\(image.fileType())")
        }
        return
    }
    super.paste(sender)
}

The code works fine if I copy an image from Photos, but when I tried pasting an image that I copied before on Safari what is pasted is the image URL.

Any idea bout how to fix that issue? I've made some research but I couldn't find anything about it.

Answered by Claude31 in 706698022

I get the same problem, not found yet how to cope with it.

Accepted Answer

I get the same problem, not found yet how to cope with it.

UIPasteboard: pasting images from Safari
 
 
Q