FileManager.containerURL(forSecurityApplicationGroupIdentifier:) returns nil on macOS

I keep getting crash reports in Xcode for one of my macOS apps published on the App Store. Actually it's not the main app that crashes, but the embedded Finder Sync extension. The crash reports indicate that this source code line

static var appGroupSaveDirectoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: identifier)!.appendingPathComponent("Library/Application Support/somedata")

crashes with error

Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value + 0 (<compiler-generated>:0)

That line is a static variable defined in a class that is included in the main app as well as the Finder extension.

The documentation reads

In iOS, the value is nil when the group identifier is invalid. In macOS, a URL of the expected form is always returned, even if the app group is invalid

If the documentation says that the method call can never be nil, why am I getting this crash? Is it a bug or is the documentation wrong, or am I doing something wrong? And why does the Finder Sync extension crash and not the main app? I cannot reproduce this crash with the App Store app or within Xcode and Console shows no crash reports for the app on my Mac.

The documentation reads

Hmmm, that’s far from the full story. For all the gory details, see App Groups: macOS vs iOS: Fight!

In short, in a sandboxed app, your app group access must be authorised by entitlements. If it’s not, this will return nil.

I’d appreciate you filing a bug against the docs; please post your bug number here, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for your help. I've created FB13238546.

I already have an App Group entitlement with value $(TeamIdentifierPrefix)org.domain.app for the main app and the extension. I cannot reproduce the crash myself, not with the App Store app and not in Xcode.

I've created FB13238546.

Thanks.

I already have an App Group entitlement

OK. That makes this issue much harder to explain.

I cannot reproduce the crash myself, not with the App Store app and not in Xcode.

Are you in touch with a user who can reproduce this? Or are you working solely from crash reports?

If it’s the latter, one good trick here is to update your containerURL(forSecurityApplicationGroupIdentifier:) code to check for nil and, if it gets that, post a notification asking for the user to trigger a sysdiagnose log and get in touch with you. That way your code doesn’t crash and you can work with the user to figure out what’s causing this odd behaviour.

Depending on the character of your user base you can either leave this enabled all the time or enable it only in your TestFlight builds.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks, I will consider asking the user to contact me when the crash happens. I only have the crash reports from Xcode. Since the whole app extension depends on the existence of that directory, it doesn’t really make sense for me to prevent the crash.

FileManager.containerURL(forSecurityApplicationGroupIdentifier:) returns nil on macOS
 
 
Q