我正在使用一个 Siri 应用程序。我在 plist
中保存数据并检索联系人。但无法获取 plist
日期。这是代码:
我在这里获取保存的数据,路径是:
/var/mobile/Containers/Data/Application/EB3CE9B8-EECC-459E-9AF0-749E10C26F95/Documents/data.plist
但是当我使用 siri 和在我的代码中尝试获取 plist 数据时,它的打印空值也是一样的:
i tried nsuserdefault also.And the path is :
/var/mobile/Containers/Data/PluginKitPlugin/80E47164-FC1F-4DBA-AAD5-D0F37107BD42/Documents/data.plist
不确定我在哪里遇到问题。但是当我尝试从我的 plist 数据中获取我的应用程序委托(delegate)中的数据时。我正进入(状态。但是为什么不使用 Siri 我没有获取联系人数据。甚至 NSUserDefault
也无法正常工作。
注意:当我使用 Siri 时,我的应用程序将在后台运行、终止或可能处于事件状态。
提前致谢!
更新:
我试过:
保存数据
let defaults = UserDefaults(suiteName: "group.com.example.app")
defaults?.set(23, forKey: "siridata")
阅读:
let defaults = UserDefaults(suiteName: "group.com.example.app")
let returnValue = defaults?.double(forKey: "siridata")
它不打印 23
。它打印 0.0
在我的目标中:
My app name group : group.com.example.app
My PaymentIntentExtension : group.com.example.app
我在 consle 中得到这个:
[User Defaults] Couldn't read values in CFPrefsPlistSource<0x101531c40> (Domain: group.com.example.app, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd
最佳答案
Intents 扩展和主应用位于单独的沙箱中,因此您需要通过“应用组”使用共享容器。
这是一篇介绍如何操作的文章:https://www.atomicbird.com/blog/sharing-with-app-extensions
在主应用目标和扩展目标的功能中设置应用组后,您可以使用 containerURL(forSecurityApplicationGroupIdentifier:)
https://developer.apple.com/documentation/foundation/filemanager/1412643-containerurl
共享 UserDefaults 的工作方式类似,同样使用 App Groups 和 init(suiteName:)
https://developer.apple.com/documentation/foundation/userdefaults/1409957-init
关于ios - 无法使用 siri 读取 plist 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51761248/