Modify SCPreferences persistent storage: Invalid A

2019-08-16 13:02发布

问题:

Currently trying to modify /Library/Preferences/com.apple.Bluetooth.plist in order to programmatically remove Bluetooth devices and running into an error.

First of all, when SCPreferencesSetValue is called, System Configuration says

Error Domain=com.apple.SystemConfiguration Code=0 "Success!" UserInfo={NSDescription=Success!}

However, when I try to SCPreferencesCommitChanges, the framework tells me

Error Domain=com.apple.SystemConfiguration Code=1002 "Invalid argument" UserInfo={NSDescription=Invalid argument}

I don't understand how setting a value succeeds but committing a value with a regular preferences object fails... Any help would be appreciated. Thank you!


var authItem = AuthorizationItem(name: kAuthorizationRuleIsAdmin, valueLength: 0, value: nil, flags: 0)
var authRights = AuthorizationRights(count: 1, items: &authItem)
let flags : AuthorizationFlags = [.interactionAllowed, .extendRights, .preAuthorize, .extendRights]
let osStatus = AuthorizationCopyRights(self.authRef!, &authRights, nil, flags, nil)

let preferences = SCPreferencesCreateWithAuthorization(kCFAllocatorDefault, "defaults" as CFString, "/Library/Preferences/com.apple.Bluetooth.plist" as CFString, authRef)        

if !(SCPreferencesSetValue(preferences!, "PairedDevices" as CFString, paired as CFPropertyList)) {
    return false
}

print("Set Value: \(SCCopyLastError())")

let ok = SCPreferencesCommitChanges(preferences!)
if !ok {
    print("Commit Changes: \(SCCopyLastError())")
}