Ios 确定复制到UIPasteboard是否成功

Ios 确定复制到UIPasteboard是否成功,ios,objective-c,ios8,uipasteboard,custom-keyboard,Ios,Objective C,Ios8,Uipasteboard,Custom Keyboard,我正在以编程方式将图像复制到UIPasteboard,我想确定复制是否成功。具体来说,我在iOS 8上创建了一个自定义键盘,其中一些键会将图像复制到粘贴板上,供用户粘贴到文本字段中 UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; [pasteBoard setImage:[UIImage imageNamed:anImage]]; 为此,用户必须允许对键盘进行“完全访问”。因此,我必须有一种方法来确定是否启用了完全访问(

我正在以编程方式将图像复制到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
在运行时是否有任何方法可以捕捉到这一点


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

我现在似乎找到了解决办法。这是我能找到的唯一正确解决问题的帖子

- (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
。然后,此方法将尝试与键盘的主应用程序通信。如果成功,那么我们可以断定完全访问已打开


我希望这个解决方案能帮助其他人,直到苹果(希望)增加一个更快的检查,看看用户是否启用了完全访问。更不用说,希望它们能为用户创建一种更简单的方式,使其能够在设置菜单之外完全访问

我现在似乎找到了解决办法。这是我能找到的唯一正确解决问题的帖子

- (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
。然后,此方法将尝试与键盘的主应用程序通信。如果成功,那么我们可以断定完全访问已打开


我希望这个解决方案能帮助其他人,直到苹果(希望)增加一个更快的检查,看看用户是否启用了完全访问。更不用说,希望它们能为用户创建一种更简单的方式,使其能够在设置菜单之外完全访问

我在swift中这样做:

func isOpenAccessGranted() -> Bool {
    return UIPasteboard.generalPasteboard().isKindOfClass(UIPasteboard)
}
也应在Obj-C中工作:

- (BOOL)isOpenAccessGranted() {
    return [[UIPasteboard generalPasteboard] isKindOfClass:UIPasteboard.class];
}

我在斯威夫特做这件事:

func isOpenAccessGranted() -> Bool {
    return UIPasteboard.generalPasteboard().isKindOfClass(UIPasteboard)
}
也应在Obj-C中工作:

- (BOOL)isOpenAccessGranted() {
    return [[UIPasteboard generalPasteboard] isKindOfClass:UIPasteboard.class];
}

谢谢,蒂姆卡森。对我来说非常有效。对于阅读本文的其他人:确保在应用程序和扩展目标中都选中了“group.com.yourdomain.yourapp”。这个复选框在Xcode 6.1 GUI中很小,所以很难看到。这是一个古老的答案。检查iOS8及以上版本的正确方法是使用ISOPENAccessGrantedThank,timgcarlson。对我来说非常有效。对于阅读本文的其他人:确保在应用程序和扩展目标中都选中了“group.com.yourdomain.yourapp”。这个复选框在Xcode 6.1 GUI中很小,所以很难看到。这是一个古老的答案。检查iOS8及以上版本的正确方法是使用IsoPenAccess