Reputation: 1261
In my mac OS X application, i have used NSUserDefaults to store values in session. This NSUserDefaults keep the session values only for respective system level users. But now i want to use session value that is common for all system level users. If i store a session value from admin, it should be accessed by any system users(For Ex: Guest User).
Suggest your answers to achieve this.
EDITED CONTENT:
I have used CFPreferences to store the session value for all system users the in reference with the michael's answer.
Code:
To save values in CFPreferences
CFStringRef appID = CFSTR("com.company.test");
CFStringRef textColorKey = CFSTR("test123");
CFStringRef colorBLUE = CFSTR("BLUE");
CFPreferencesSetValue (textColorKey,
colorBLUE,
appID,
kCFPreferencesAnyUser,
kCFPreferencesCurrentHost
);
CFPreferencesSynchronize(appID,
kCFPreferencesAnyUser,
kCFPreferencesCurrentHost);
To retrieve from CFPreferences
CFStringRef textColorKey = CFSTR("test123");
CFStringRef textColor;
// Read the preference.
CFStringRef appID = CFSTR("com.company.test");
textColor = (CFStringRef)CFPreferencesCopyValue(textColorKey,
appID,
kCFPreferencesAnyUser,
kCFPreferencesCurrentHost);
But it always retrieve nil value. Please point out what i am missing here.
Upvotes: 1
Views: 242
Reputation: 89549
If you look within the first few paragraphs of the NSUserDefaults documentation, you'll see this tidbit:
The NSUserDefaults class does not currently support per-host preferences. To do this, you must use the CFPreferences API (see Preferences Utilities Reference). However, NSUserDefaults correctly reads per-host preferences, so you can safely mix CFPreferences code with NSUserDefaults code.
CFPreferences does have a kCFPreferencesAnyUser
key, but it's also a pain in the rear end to use for what you are trying to do. Other people have suggested workarounds that involve hiding files in locations that are readable and writable to all users on the machine, but if you really want to do this right, you'd need to come up with a solution that involves writing a separate tool (which gets root/admin privileges) that sets these privileges, called via SMJobBless from your app.
Upvotes: 1