我现在似乎找到了解决方案。这来自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 [希望] 更快地检查用户是否启用了完全访问权限。更不用说,希望他们为用户创建一种更简单的方式来启用设置菜单之外的完全访问权限。