4

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?

Community
  • 1
  • 1
IluTov
  • 6,807
  • 6
  • 41
  • 103

3 Answers3

4

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.

diederikh
  • 25,221
  • 5
  • 36
  • 49
  • Does this mean that the `NSUserDefaults` will be shared automatically? Apple's documentation doesn't provide too much information. – IluTov Dec 23 '12 at 20:47
  • The preferences are part of the sandbox. – diederikh Dec 24 '12 at 09:22
  • It didn't work though. I have also tried it with `addSuiteNamed:@"com.MyApp.TheGUIPartOfMyApp"`... – IluTov Dec 24 '12 at 09:23
  • It did work, sorry it was actually my bad. I did the initialising of the observers at the beginning of the helper app, so it didn't update if I toggled the flag in the main app. – IluTov Dec 24 '12 at 09:27
4

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.

Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168
1

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.

  • I'm not sure how this should help me. This only sets a value of a key... I wanna know how to share `NSUserDefaults` between two applications. – IluTov Dec 23 '12 at 20:48
  • No prob. Merry christmas ;) – IluTov Dec 23 '12 at 20:50
  • Seems like its impossible unless the device was jailbreaked –  Dec 23 '12 at 21:56
  • It's not iOS, it's OSX. It should work using **Application Groups**, which I'm looking into. Thanks anyway. – IluTov Dec 23 '12 at 21:59
  • Ok, but I don't want to use iCloud because of one shared property... Take a look at **Diederik Hoogenboom** s answer. Maybe you know what's going wrong there. – IluTov Dec 23 '12 at 22:07
  • This is another question of me: http://stackoverflow.com/questions/14014192/how-can-i-terminate-my-app-in-a-helper-app-with-sanboxing-enabled Same solution, there you can see why it does't work for me. – IluTov Dec 23 '12 at 22:13