问题
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