简体   繁体   中英

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. 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...

"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?

I'm hitting this same issue. I was using an iOS-style App Group: 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?

[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. For example: DG29478A379Q6483R9214.HolstFirstAppSuite

Apple Source:

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.

Note the lack of "." , which is apparently included in the $Prefix. If you add your own "." the App Group ends up with ".."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM