10

Is it possible to copy text/image to UIPasteboard in a keyboard extension? Similar to what popkey.co does with animated images.

I tested the following code and it doesn't seem to be working.

func copyImage() {
    UIPasteboard.generalPasteboard().string = "copy test"
}

It always shows this error message:

UIPasteboard - failed to launch pasteboardd. Make sure it's installed in UIKit.framework/Support

Do you know about any other way to use copy&paste from a keyboard extension?

John Komarek
  • 250
  • 4
  • 9

2 Answers2

23

I was able to do it if I gave my custom keyboard "Full Access" in the Settings->General->Keyboard app. You need to include "RequestsOpenAccess" = YES in your Info.plist file. AND you have to toggle "Full Access" on in the Settings app.

Seems like Apple is restricting access to the general UIPasteboard otherwise.

Jonathan Arme
  • 246
  • 2
  • 2
  • Thank you for your advice. I've tried to add it to the Info.plist file but still no luck. I don't get the error anymore (even with the original file for some reason) but `UIPasteboard.generalPasteboard()` returns nil. – John Komarek Sep 23 '14 at 16:47
  • 8
    You're right, it was an issue of the "full access". I added it to root of the Info.plist but it must be done in **Info.plist -> NSExtension -> NSExtensionAttributes -> RequestsOpenAccess -> YES** Thanks for your help! – John Komarek Sep 23 '14 at 21:55
  • It has to be set in the info.plist of your keyboard extension, not from your containing app – Jasper Oct 02 '14 at 14:57
  • Thanks for this answer. – Jagveer Singh Jan 11 '16 at 06:46
2

First of all you have to get full access to your custom keyboard for use images / gifs ... on the iPhone Settings -> General -> Keyboards -> Keyboards -> Add New Keyboard... (Select your keyboard under THIRD-PARTY KEYBOARDS) -> click on your keyboard and toggle Allow Full Access

To do that you have to go to the to set RequestsOpenAccess = YES in the info.plist located in the keyboard extension folder.

Info.plist -> NSExtension -> NSExtensionAttributes -> RequestsOpenAccess -> YES

The following method will get the button tag check the tag in the switch statement and set the correct image according to the button tag to the pasteboard.

func btnPressed(sender: AnyObject) {

    var btn = sender as UIButton

    switch (btn.tag){
    case 5:
       let imageURL = NSBundle.mainBundle().pathForResource("cat", ofType: "png")
       let data = NSData(contentsOfURL: NSURL(fileURLWithPath: imageURL!)!);
       UIPasteboard.generalPasteboard().setData(data!, forPasteboardType: "public.png")

    case 10:
       let imageURL = NSBundle.mainBundle().pathForResource("dog", ofType: "png")
       let data = NSData(contentsOfURL: NSURL(fileURLWithPath: imageURL!)!);
       UIPasteboard.generalPasteboard().setData(data!, forPasteboardType: "public.png")
   }}

User then can paste the image to any supported application...

Hope this helps !

Ankahathara
  • 2,866
  • 2
  • 20
  • 26