ios - 如何测试包含应用程序是否授予 "Allow Full Access"权限?

标签 ios swift keyboard ios-keyboard-extension

我正在做一个键盘扩展项目。在应用程序代码的某些点,我需要测试用户是否已授予键盘扩展的“允许完全访问”权限。交易是我需要从应用程序端进行这些测试,并基于此让用户访问键盘设置或在未授予权限时提醒他。

问题是这里提供的方法像:

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

或者:

func isOpenAccessGranted() -> Bool {
let fm = NSFileManager.defaultManager()
let containerPath = fm.containerURLForSecurityApplicationGroupIdentifier(
                    "group.com.example")?.path
var error: NSError?
fm.contentsOfDirectoryAtPath(containerPath!, error: &error)
if (error != nil) {
    NSLog("Full Access: Off")
    return false
}
NSLog("Full Access: On");
return true
}

仅在键盘端工作,因为键盘是唯一受此权限影响的键盘。从包含应用程序端,这两种方法始终返回 true。

有人知道从应用端测试这个的可靠方法吗?

最佳答案

考虑在您的应用和键盘中使用 NSUerdefaults。为了让扩展和应用程序能够共享相同的 NSUserdefaults,您可以简单地打开应用程序组。在项目导航器中选择项目下的主要应用程序目标,然后转到功能并通过将其切换为打开来启用应用程序组,添加您的开发人员资料并修复可能出现的问题。 Sceenshot 现在创建一个新容器。根据帮助,它必须以“group.”开头,因此将其命名为“group.com.mycompany.myapp”。选择您的 Today Extension 目标并重复此打开应用程序组的过程。不要创建新的,而是选择这个新创建的组来表示 Today Extension 是该组的一部分。

这将允许您与容器应用程序共享相同的 NSUserdefaults。 您现在所要做的就是使用您提供的代码并将保存方法添加到 NSUserdefaults 中,如下所示:

func isOpenAccessGranted() -> Bool {
    let defaults = NSUserDefaults.standardUserDefaults()
    let fm = NSFileManager.defaultManager()
    let containerPath = fm.containerURLForSecurityApplicationGroupIdentifier(
        "group.com.example")?.path
    var error: NSError?
    fm.contentsOfDirectoryAtPath(containerPath!, error: &error)
    if (error != nil) {
        NSLog("Full Access: Off")
        defaults.setBool(false, forKey: "hasFullAccess")
        return false
    }
    NSLog("Full Access: On");
    defaults.setBool(true, forKey: "hasFullAccess")
    return true
}

在您的键盘扩展中执行此操作。然后在您的父应用程序中,只需检索它们并根据它们的值采取行动。

let hasFullAccess : Bool = NSUserDefaults.standardUserDefaults().boolForKey("hasFullAccess")


if hasFullAccess{
    //User granted full access
}
else{
    //User didn't grant full access
}

如果用户没有授予完全访问权限,则显示警告,否则代码消失!

编辑: 在联系 Apple 的技术开发人员支持后,他们告诉我,目前无法以任何支持的方式实现这一点。他们的回复如下:

Our engineers have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations.

If you would like for Apple to consider adding support for such features in the future, please submit an enhancement request via the Bug Reporter tool at https://developer.apple.com/bug-reporting/.

希望对你有所帮助,朱利安

关于ios - 如何测试包含应用程序是否授予 "Allow Full Access"权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119938/

相关文章:

angular - ionic 2 - 单击发送按钮时保持键盘打开

ios - 检测 UIbuttons 是否重叠

ios - 使用 AVFoundation 的方形视频

ios - Xcode 中的 "app"项目和 "framework"项目有什么区别?

ios - UICollectionView 显示隐藏动画问题

macos - 如何检测 OS X 中大写锁定键的按下或释放?

iphone - Howe 捕获 UIView 顶部 UIView

ios - CNContactViewController() 的 "Create New Contact"和 "Add to Existing Contact"

xcode - 使用 NSTimer 创建 Neat 延迟函数 - Swift

java - 如何更新我的 Java 程序以仅显示两位小数