MeIr
MeIr

Reputation: 7316

NSUserDefaults of different application

I am currently developing System Pane and my app have some configuration settings saved to User Defaults:

NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:listOfStuff];
[userDefault setObject:encodedObject forKey:@"myStuff"];
[userDefault synchronize];

Can anyone tell me if and how a different application can read settings that have been saved in above System Pane?

Thank you.

Upvotes: 2

Views: 2376

Answers (4)

Thomas Tempelmann
Thomas Tempelmann

Reputation: 12043

My app Prefs Editor uses the CFPreferences functions for this (e.g. CFPreferencesSetAppValue). That gives you direct control over whose app's settings you'll access.

You can even access the prefs of sandboxed applications with this method, if you know their plist file's full path and pass that to the applicationID parameter.

For more info, read through the answers of this question: How does OS X's defaults command get access to prefs of sandboxed apps?

Upvotes: 0

0xced
0xced

Reputation: 26478

As Melr explained, you need to use the suite name. But it's actually possible to read the preferences of another application from a sandboxed app. It requires a temporary exception entitlement.

For example, to read the Finder preferences, you need to add this to your entitlements file:

<key>com.apple.security.temporary-exception.shared-preference.read-only</key>
<array>
    <string>com.apple.finder</string>
</array>

Then you can access the Finder preferences like this:

NSUserDefaults *finderDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.apple.finder"];
NSDictionary *standardViewOptions = [finderDefaults dictionaryForKey:@"StandardViewOptions"];
NSNumber *fontSize = [standardViewOptions valueForKeyPath:@"ColumnViewOptions.FontSize"];

If you need to read the preferences of another app, just replace com.apple.finder with the appropriate bundle identifier.

Upvotes: 2

MeIr
MeIr

Reputation: 7316

The way to read someones preferences is very simple and straight forward:

    NSUserDefaults *defaults = [[NSUserDefaults alloc] init];

    [defaults addSuiteNamed:@"com.apple.systempreferences.plist"];

    NSLog(@"DefaultExposeTab is: %@", [defaults stringForKey:@"DefaultExposeTab"]); 

Make sure to initialize NSUserDefaults following way:

[[NSUserDefaults alloc] init];

then you can add desired preference list, in our case I would like to read System Preferences:

[defaults addSuiteNamed:@"com.apple.systempreferences.plist"];

and finally get value for whichever key you want, in this example:

"DefaultExposeTab"

Above example works like a charm. Please remember it will only work for current user.

Thanks.

P.S: Please note - above example will NOT work for sandboxed application.

Upvotes: 6

Fruity Geek
Fruity Geek

Reputation: 7381

Yes another application can read the settings if you save by using NSUserDefaults because it saves the settings as an unencrypted plist file on disk.

Reading another application's settings from a plist file is easy. Search for the file path and open it. NSDictionary can be initialized with a string of the file path to a plist file.

If the defaults you want to store are secret (like password, security tokens, etc), then add an encrypted item to the keychain instead of storing them in a plist.

https://developer.apple.com/library/mac/documentation/security/Conceptual/keychainServConcepts/01introduction/introduction.html

Upvotes: -2

Related Questions