Reading NSUserDefaults from helper app in the sandbox

混江龙づ霸主 提交于 2019-12-03 16:44:33

Since 10.7.4 you can use Application Groups within the sandbox. All applications within the group share the same sandbox. See Application Groups on how to set these up.

It's possible to share preferences between a main app and helper app using Security Application Groups and -[NSUserDefaults initWithSuiteName:]:

Security Application Groups

In order for multiple apps to share a common container, you'll want to set the com.apple.security.application-groups entitlement (in your main and helper app) to a common identifier, such as @"com.company.my-app-suite". See Adding an App to a Group for more information.

User Defaults Suites

As per the Foundation Release Notes for OS X 10.9:

For applications that are part of a Security Application Group, the NSUserDefaults "suite" APIs (-initWithSuiteName:, -addSuiteNamed: and -removeSuiteNamed:) will operate on a suite shared by applications in the group and stored in the group container, if the suite identifier is the identifier of the group.

So you'll want to do something like this in your application delegate (or similar):

- (NSUserDefaults *)sharedUserDefaults {
    static NSUserDefaults *shared = nil;
    if (!shared) {
        shared = [[NSUserDefaults alloc] initWithSuiteName:@"com.company.my-app-suite"];
    }
    return shared;
}

And use that instead of [NSUserDefaults standardUserDefaults] throughout both your apps.

Apps can share a container directory on iCloud.

From Apple's doc on configuring your iCloud entitlements:

The iCloud Containers field identifies the list of container directories that your app can access in the user’s iCloud storage. (This field corresponds to the com.apple.developer.ubiquity-container-identifiers entitlement.) The strings you add to this list must correspond to bundle identifiers for apps created by your team. Xcode uses the current app’s bundle identifier to specify the first string; you can change this to a different bundle identifier if you want multiple apps to share a main container directory. You can also add additional bundle identifiers for your team’s other apps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!