Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 Custom Keyboard - Copy & Paste to UIPasteboard

Tags:

uikit

swift

ios8

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?

like image 935
John Komarek Avatar asked Sep 21 '14 08:09

John Komarek


2 Answers

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.

like image 176
Jonathan Arme Avatar answered Nov 20 '22 03:11

Jonathan Arme


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 !

like image 2
Ankahathara Avatar answered Nov 20 '22 04:11

Ankahathara