Search code examples
macosxcode5nsuserdefaults

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.


Solution

  • 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.