如何测试是否“允许完全访问权限"权限是从包含应用程序授予的? [英] How to test if "Allow Full Access" permission is granted from containing app?

查看:226
本文介绍了如何测试是否“允许完全访问权限"权限是从包含应用程序授予的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究键盘扩展项目.在应用程序代码的某些点上,我需要测试用户是否已授予键盘扩展的允许完全访问"权限.要做的是,我需要在应用程序端进行这些测试,并在此基础上让用户访问键盘设置或在未授予许可的情况下提醒他.

I'm working on a keyboard extension project. At some points of the application code I need to test if the user have granted the "Allow Full Access" permission for the keyboard extension. The deal is that I need to do those tests from the application side, and based on this let the user to access keyboard settings or alert him in case the permission wasn't granted.

问题在于,此处提供的方法如下:

The problem is that the methods that provided here like:

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.

Working only from the keyboard side, as the keyboard is the only one that is affected from this permission. From the containing app side both of those methods always return true.

有人知道从应用程序角度进行测试的可靠方法吗?

Does someone knows a reliable way to test this from the application side?

推荐答案

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

Consider using NSUSerdefaults in your app and keyboard. In order for an extension and app to be able to share the same NSUserdefaults, you can simply turn on App Groups. Select your main app target under your project in the project navigator and go to capabilities and enable App Groups by toggling it to on, adding your Developer profile and fixing the possibly arising issues. Now create a new container. According to the help, it must start with "group.", so give it a name like "group.com.mycompany.myapp". Select your Today Extension target and repeat this process of switching on app groups. Don’t create a new one, rather select this newly created group to signify that the Today Extension is a part of the group.

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

This will now allow you to share the same NSUserdefaults as your container app. All you have to do now is use the code you provided and add the saving method to the NSUserdefaults like so:

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
}

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

Do this in your keyboard extension. And then in your parent app, simply retrieve them and act upon their value.

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


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

如果用户未授予完全访问权限,则显示警报,否则删除代码!

If the user didn't grant full access the show an alert, else code away!

与Apple技术开发人员联系后,他们告诉我,到目前为止,这不可能以任何受支持的方式实现.他们的回应如下:

After contacting Apple's Technical Developer support they told me that this is not possible to achieve in any supported way as of right now. Their response is below:

我们的工程师已审核您的要求,并得出结论: 在给定的条件下,不支持实现所需功能的方法 当前发布的系统配置.

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.

如果您想让Apple考虑添加对此功能的支持 将来的功能,请通过 位于 https://developer.apple.com/bug-reporting/的Bug Reporter工具.

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/.

希望有帮助,朱利安

这篇关于如何测试是否“允许完全访问权限"权限是从包含应用程序授予的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆