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

橙三吉。 提交于 2019-12-03 14:22:50

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

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