Sending images with iOS 8 custom keyboard?

2019-03-27 18:34发布

问题:

I've been developing a custom keyboard for iOS 8, but stumbled upon a problem trying to send images using the keyboard. I did some research and it seems like there isn't a simple way to do this with UITextDocumentProxy because only NSStrings are allowed.

Am I either overlooking any simple ways to use a custom keyboard to send images and/or is there any way to work around this issue?

Thanks in advance

回答1:

Apparently, you are not the only person to try a keyboard like this. If you look at the animated GIF on the site, the keyboard uses copy/paste to add the image to the messages.

The UIKeyInput Protocol, which is inherited by UITextDocumentProxy, has the following function:

func insertText(_ text: String)     //Swift
- (void)insertText:(NSString *)text //ObjC

These only take a String or an NSString, as you already know. Because there are no other methods to insert content, it can be assumed that, currently, there is no other way.

Right now, I would suggest that you adopt a similar usage. When the user taps on the image, add it to the UIPasteboard. Maybe present a little banner on top of the keyboard saying, "You can now paste" or something, but that would be up to you to decide.