How to test if “Allow Full Access” permission is granted from containing app?

孤街醉人 提交于 2019-12-21 04:59:04

问题


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

or:

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
}

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?


回答1:


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.

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!

EDIT: 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.

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

Hope that helps, Julian



来源:https://stackoverflow.com/questions/32119938/how-to-test-if-allow-full-access-permission-is-granted-from-containing-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!