简体   繁体   English

为什么基于应用程序组(即suiteName)的NSUserDefaults返回不同的实例?

[英]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.

相关问题 在Xcode 6.1(Swift)中将NSUserDefaults与suiteName一起使用时出现问题 - Trouble using NSUserDefaults with suiteName in Xcode 6.1 (Swift) 如何以编程方式嵌入视图(即组UI元素) - How to embed in view (i.e. group UI elements) programmatically 仅针对Iphone 6 Plus自动调整应用程序大小(即不针对Iphone 6)? - Auto app-resize for Iphone 6 Plus only (i.e. not Iphone 6)? 为什么XCode吓坏了? 也就是说,Xcode表示方法和属性不存在,但它们确实存在! - Why is XCode freaking out?? I.e., Xcode says methods & properties do not exist but they do! NSuserDefaults不保存,直到我关闭应用程序迅速 - NSuserDefaults not saving until i close the app swift WatchKit扩展程序没有看到针对App Group的NSUserDefaults的更新 - WatchKit extension doesn't see updates to NSUserDefaults for App Group 简单的代码,以获得托管应用程序的Mac类型? 即“MacBook Pro(Retina,15英寸,2013年末)” - Simple code to get type of Mac that's hosting app? i.e. “MacBook Pro (Retina, 15-inch, Late 2013)” NSUserDefaults返回(空) - NSUserDefaults returning (null) Xcode 9问题重复(“未分类” +即“快速编译器错误”) - Xcode 9 issue duplication ('Uncategorized' + i.e. 'Swift Compiler Error') main.m的编辑器,保留格式,即颜色 - editor for main.m that retains formatting, i.e. colors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM