Reading NSUserDefaults from helper app in the sandbox

我的未来我决定 提交于 2019-12-05 00:51:36

问题


I found some resources on reading the NSUserDefaults of another application.

Objective-C NSUserDefaults caching prevents another app from accurately reading changes

NSUserDefaults: Is it possible to get userDefaults from another app?

Apparently, it's not possible. However the questions firstly relate to iOS, and secondly the two apps are completely different.

I have a LaunchAtLogin helper app. But it does some other tasks too. Therefore, the helper app should run always, but only start the main app if the BOOL in the NSUserDefaults is set.

Is there a way I can achieve that?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/14014417/reading-nsuserdefaults-from-helper-app-in-the-sandbox

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