1

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

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

为此,用户必须在键盘上允许“完全访问”。所以我要么必须有办法确定是否完全访问(不知道如何检查),要么确定复制到粘贴板是否成功。如果完全访问没有打开,我必须提醒用户打开它才能让键盘工作。

当复制失败时(由于完全访问关闭),我从 UIPasteboard 收到日志消息:

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

无论如何在运行时捕捉到这个?

任何有关如何实现这一目标的建议将不胜感激!

4

2 回答 2

3

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

- (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;
    }
}

为了使其工作,您必须配置一个应用程序组,您的键盘扩展将使用该应用程序组来尝试与您的键盘应用程序进行通信。为此,请按照 Apple 关于配置应用程序组的说明进行操作。使用您在此处创建的标识符替换yourAppGroupID上述代码中的字符串。然后此方法将尝试与您的键盘的主应用程序进行通信。如果成功,那么我们可以得出结论,完全访问已打开。

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

于 2014-09-28T19:19:49.183 回答
3

我正在迅速这样做:

func isOpenAccessGranted() -> Bool {
    return UIPasteboard.generalPasteboard().isKindOfClass(UIPasteboard)
}

也应该在 Obj-C 中工作:

- (BOOL)isOpenAccessGranted() {
    return [[UIPasteboard generalPasteboard] isKindOfClass:UIPasteboard.class];
}
于 2015-05-21T10:47:55.233 回答