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 中,如下所示:
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
}
如果用户未授予完全访问权限,则会显示警报,否则请编码!
EDIT:在联系苹果的技术开发人员支持后,他们告诉我,目前无法以任何支持的方式实现这一点。他们的回应如下:
我们的工程师已审查您的请求并得出结论:
鉴于以下情况,不支持实现所需功能的方法
目前正在运送系统配置。
如果您希望 Apple 考虑添加对此类的支持
未来的功能,请通过以下方式提交增强请求
错误报告工具位于https://developer.apple.com/bug-reporting/ https://developer.apple.com/bug-reporting/.
希望有帮助,朱利安