You can do this with CFPreferences, specifying kCFPreferencesAnyUser. Historically it was possible to use this to set a pref as an admin user without authenticating but that no longer works because we’ve been tightening up file system permissions. However, you seem to be happy with a command-line interface and so it’s easy to authenticate the set side using sudo.
Here’s some code snippets:
// to get
let greetingWithDate = "Hello Cruel World! \(Date())"
CFPreferencesSetValue(
"greetingWithDate" as NSString,
greetingWithDate as NSString,
"com.example.apple-samplecode.Test700292" as NSString,
kCFPreferencesAnyUser,
kCFPreferencesCurrentHost
)
print("did set `greetingWithDate` in `com.example.apple-samplecode.Test700292` to '\(greetingWithDate)'")
// to get
let propQ = CFPreferencesCopyValue(
"greetingWithDate" as NSString,
"com.example.apple-samplecode.Test700292" as NSString,
kCFPreferencesAnyUser,
kCFPreferencesCurrentHost
)
guard let prop = propQ else {
print("no property")
… bail …
}
guard let str = (prop as? String) as String? else {
print("wrong type")
… bail …
}
print("did get `greetingWithDate` in `com.example.apple-samplecode.Test700292` as '\(str)'")
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"