[英]Why are app group (i.e. suiteName)-based NSUserDefaults returning different instances?
Kinda stumped by this. 有点难过这个。 I'm working on an Xcode extension that needs to communicate with its container app via NSUserDefaults.
我正在开发一个Xcode扩展,需要通过NSUserDefaults与其容器应用程序进行通信。 To do this, you need to set up an app group entitlement in both the extension and the container app, sign your app, then initialize the defaults like so...
为此,您需要在扩展程序和容器应用程序中设置应用程序组权利,签署您的应用程序,然后初始化默认值,如此...
let defaults = UserDefaults(suiteName: "group.com.MyCo.TestCode")!
The problem is even though no errors are thrown, the extension and the app get two distinct copies of the defaults. 问题是即使没有抛出错误,扩展和应用程序也会获得两个不同的默认副本。
However, if I go back and when adding the group, leave the TeamIdentifierPrefix in place, I end up with a group name of this instead... 但是,如果我返回并在添加组时,保留TeamIdentifierPrefix,我最终会得到一个组名,而不是...
"03V7NBU5AX.group.com.MyCo.TestCode"
Which I use in the initialization like this... 我在这样的初始化中使用哪个...
let defaults = UserDefaults(suiteName: "03V7NBU5AX.group.com.MyCo.TestCode")!
...and now everything works! ......现在一切正常! But why didn't the first case work?
但为什么第一个案例不起作用呢? Every tutorial I've seen says as long as the app groups match it should work, and not a single tutorial I've seen mentions the team identifier prefix.
我看到的每个教程都说只要应用程序组匹配它就可以工作,而不是我见过的单个教程提到团队标识符前缀。 Is this something specific to Xcode extensions?
这是Xcode扩展特有的吗?
I'm hitting this same issue. 我正在遇到同样的问题。 I was using an iOS-style App Group:
group.com.MyCompany.MyTarget
我使用的是iOS风格的App组:
group.com.MyCompany.MyTarget
This seemed to be working in most cases. 这似乎在大多数情况下都有效。 But then certain App Group functionality suddenly stopped working, or perhaps was not working all along.
但是,某些应用程序组功能突然停止工作,或者可能一直没有工作。
A little research turned up Mac-specific App Group naming requirements of which I was not previously aware: Should macOS App Group name start with `group.` or Development Team ID? 一个小小的研究发现了Mac特定的App Group命名要求,我以前没有意识到: macOS App Group名称应该以`group。还是开发团队ID开头?
[The App Group ID] must consist of your development team ID , followed by a period, followed by an arbitrary name chosen by your development team.
[应用程序组ID]必须包含您的开发团队ID ,后跟一段时间,然后是开发团队选择的任意名称。 For example:
DG29478A379Q6483R9214.HolstFirstAppSuite
例如:
DG29478A379Q6483R9214.HolstFirstAppSuite
Apple Source: Apple来源:
https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW19 https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW19
I switched my App Group name to: $(TeamIdentifierPrefix)MyAppGroup
in Xcode. 我将我的应用程序组名称切换为:Xcode中的
$(TeamIdentifierPrefix)MyAppGroup
。
Note the lack of "."
注意缺少
"."
, which is apparently included in the $Prefix. ,显然包含在$ Prefix中。 If you add your own
"."
如果你添加自己的
"."
the App Group ends up with ".."
应用程序组最终以
".."
结束
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.