Determine if a copy to UIPasteboard was successful or not

旧巷老猫 提交于 2019-12-04 19:07:25

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

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.

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.

I'm doing this in swift:

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

Should work in Obj-C as well:

- (BOOL)isOpenAccessGranted() {
    return [[UIPasteboard generalPasteboard] isKindOfClass:UIPasteboard.class];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!