Is it possible to copy text/image to UIPasteboard in a keyboard extension? Similar to what popkey.co does with animated images.
是否可以在键盘扩展中将文本/图像复制到UIPasteboard ?类似于popkey什么。co处理动画图像。
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
UIPasteboard -没能启动pasteboardd。确保它安装在UIKit.framework/Support中
Do you know about any other way to use copy&paste from a keyboard extension?
你知道有什么其他的方法来使用复制粘贴从键盘扩展吗?
2 个解决方案
#1
20
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.
如果我在设置->通用->键盘应用程序中给我的自定义键盘“完全访问”,我就能做到这一点。plist文件。你必须在设置应用中切换“完全访问”。
Seems like Apple is restricting access to the general UIPasteboard otherwise.
似乎苹果限制了通用UIPasteboard的访问权限。
#2
1
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
首先,您必须完全访问您的自定义键盘,以使用图像/ gif…在iPhone设置上->通用->键盘->键盘->添加新键盘…(选择你的键盘在第三方键盘下)->点击你的键盘和切换允许完全访问
To do that you have to go to the to set RequestsOpenAccess = YES in the info.plist located in the keyboard extension folder.
要做到这一点,你必须在info中设置RequestsOpenAccess = YES。位于键盘扩展文件夹中的plist。
Info.plist -> NSExtension -> NSExtensionAttributes -> RequestsOpenAccess -> YES
信息。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.
下面的方法将会得到按钮标签检查switch语句中的标签,并根据按钮标签将正确的图像设置为粘贴板。
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 !
希望这可以帮助!
#1
20
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.
如果我在设置->通用->键盘应用程序中给我的自定义键盘“完全访问”,我就能做到这一点。plist文件。你必须在设置应用中切换“完全访问”。
Seems like Apple is restricting access to the general UIPasteboard otherwise.
似乎苹果限制了通用UIPasteboard的访问权限。
#2
1
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
首先,您必须完全访问您的自定义键盘,以使用图像/ gif…在iPhone设置上->通用->键盘->键盘->添加新键盘…(选择你的键盘在第三方键盘下)->点击你的键盘和切换允许完全访问
To do that you have to go to the to set RequestsOpenAccess = YES in the info.plist located in the keyboard extension folder.
要做到这一点,你必须在info中设置RequestsOpenAccess = YES。位于键盘扩展文件夹中的plist。
Info.plist -> NSExtension -> NSExtensionAttributes -> RequestsOpenAccess -> YES
信息。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.
下面的方法将会得到按钮标签检查switch语句中的标签,并根据按钮标签将正确的图像设置为粘贴板。
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 !
希望这可以帮助!