Sandboxed Cocoa Application Groups
-
11-12-2019 - |
Question
The entitlement documentation describes a method in which multiple apps produced by a single development team can share access to a special group container
I've added the entitlement key to a main application and a helper application that is included in the main application bundle. The group directory is never created, and the method in which to get the path – [NSURL containerURLForSecurityApplicationGroupIdentifier:]
– doesn't actually exist in any header.
What am I missing to create a shared group container?
UPDATE : added entitlements and group container creation
Here is my entitlements file for the main app and helper application. (replacing TEAM_ID with my actual ID)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>TEAM_ID.com.pinepointsoftware</string>
</array>
</dict>
</plist>
I've attempted to create the Group Container directory myself based on some information from the dev forums:
NSFileManager *fm = [NSFileManager defaultManager];
NSString *path = [@"~/Library/Group Containers/TEAM_ID.com.pinepointsoftware" stringByExpandingTildeInPath];
if ([fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL]) {
[fm createFileAtPath:[path stringByAppendingPathComponent:@"test.txt"] contents:nil attributes:nil];
}
Executing that inside the main and and helper app create two different directories inside their each sandbox.
I found a sample project from Apple AppSandboxLoginItemXPCDemo, that uses the application groups for XPC. I can get it working by changing the entitlements and bundle identifiers to match my team, but cannot get the Group Containers to be shared.
Solution
The "~" will be automatically converted to your container directory in a sandboxed environment.
So one way to do it is to append the "~" with a number of step back ("../") until you reach ~/Library and then you create your group container starting from it.