user120084
user120084

Reputation: 304

One preferences for two applications

I have two applications with id-s: com.myCompany.mayApp and com.myCompany.mayAppPro. How can I use one pref file com.myCompany.mayApp.plist for two these applications? Is it possible to use class NSUserDefaults for this?

Upvotes: 4

Views: 986

Answers (2)

Jon Steinmetz
Jon Steinmetz

Reputation: 4124

Take a look at the following methods in NSUserDefaults:

- (NSDictionary *)persistentDomainForName:(NSString *)domainName;
- (void)setPersistentDomain:(NSDictionary *)domain forName:(NSString *)domainName;
- (void)removePersistentDomainForName:(NSString *)domainName;

They allow you read and write to a preferences file with a given domain name. An example is to read some common preferences for the Apple iApps:

NSUserDefaults* prefs = [ NSUserDefaults standardUserDefaults ];
NSDictionary* iAppsPrefs = [ prefs persistentDomainForName: @"com.apple.iApps" ];
NSArray* recentPaths = [ iAppsPrefs objectForKey: @"iTunesRecentDatabasePaths" ];

The previous code reads the array of recent paths for the iTunes database files.

The disadvantage of these methods are that they read and write the entire contents of the file. If the number items being stored is not really large then this is not generally a problem.

Upvotes: 5

justin
justin

Reputation: 104708

i think you'll have to use the CFPreference APIs to set shared values, but you can read the values using NSUserDefaults by adding the suite to the search path of the shared NSUserDefaults instance. of course, you may read the values using the CFPreference APIs too.

if your prefs are complex and you want to use cocoa bindings, you may as well write your own interface which wraps the keys/value/domain/host/user config.

Upvotes: 0

Related Questions