确定UIPasteboard的副本是否成功 [英] Determine if a copy to UIPasteboard was successful or not

查看:301
本文介绍了确定UIPasteboard的副本是否成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是以编程方式将图像复制到UIPasteboard,我想确定复制是否成功。具体来说,我正在iOS 8上创建一个自定义键盘,其中一些键将图像复制到粘贴板,供用户粘贴到文本域中。

I'm programmatically copying an image to the UIPasteboard, and I want to determine if the copy was successful or not. Specifically, I'm creating an custom keyboard on iOS 8, where some of the keys will copy an image to the pasteboard for the user to paste in a textfield.

UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
[pasteBoard setImage:[UIImage imageNamed:anImage]];

为此,用户必须允许键盘上的完全访问。因此,我要么必须确定是否启用了完全访问权限(不确定如何检查),或者确定复制到粘贴板是否成功。如果未启用完全访问权限,则必须提醒用户将其打开以使键盘正常工作。

To do this, the user must allow "Full Access" on the keyboard. So I either have to have a way to determine if Full Access is on (not sure how to check this), or determine if the copy to the pasteboard was successful. If Full Access isn't on, I have to alert the user to turn it on in order for the keyboard to work.

当副本失败时(由于Full访问被关闭),我从UIPasteboard获取日志消息:

When the copy does fail (due to Full Access being off), I get the log message from UIPasteboard:

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

无论如何都要在运行时捕获它吗?

Is there anyway to catch this at runtime?

任何有关如何实现这一目标的建议都将受到赞赏!

Any suggestions on how to achieve this would be appreciated!

推荐答案

我似乎现在找到了解决方案。这来自 Apple Developer论坛(用户Andrew Boyd),是我唯一的帖子可以找到正确解决问题。

I seem to have found a solution for now. This comes from the Apple Developer forums (user Andrew Boyd), and is the only post I could find that correctly solves the problem.

- (BOOL)testFullAccess
{
    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"yourAppGroupID"];  // Need to setup in Xcode and Developer Portal
    NSString *testFilePath = [[containerURL path] stringByAppendingPathComponent:@"testFullAccess"];

    NSError *fileError = nil;
    if ([[NSFileManager defaultManager] fileExistsAtPath:testFilePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:testFilePath error:&fileError];
    }

    if (fileError == nil) {
        NSString *testString = @"testing, testing, 1, 2, 3...";
        BOOL success = [[NSFileManager defaultManager] createFileAtPath:testFilePath
                                                           contents: [testString dataUsingEncoding:NSUTF8StringEncoding]
                                                         attributes:nil];
        return success;
    } else {
        return NO;
    }
}

为了实现这一点,你必须配置一个应用程序组,您的键盘扩展程序将用于尝试与您的键盘应用程序通信。为此,请按照配置应用程序组的说明操作。使用您在那里创建的标识符替换上面代码中的字符串 yourAppGroupID 。然后,此方法将尝试与键盘的主应用程序通信。如果成功,那么我们可以得出结论,Full Access已启用。

In order for this to work, you must configure an app group, which your keyboard extension will use in an attempt to communicate with your keyboard app. To do this, follow Apple's instructions on Configuring App Groups. Use the identifier you create there to replace the string yourAppGroupID in the above code. Then this method will attempt to communicate with the main app for your keyboard. If successful, then we can conclude that Full Access is on.

我希望此解决方案可以帮助其他人,直到Apple [希望]更快地检查用户是否启用了完全访问权限或不。更不用说,希望它们为用户创建一种更简单的方法,以便在设置菜单之外启用完全访问权限。

I hope this solution helps someone else until Apple [hopefully] adds a quicker check if the user enabled Full Access or not. Not to mention, hopefully they create an easier way for the user to enable full access outside of the settings menu.

这篇关于确定UIPasteboard的副本是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆