修改SCPreferences永久存储:无效参数

时间:2019-04-19 15:50:35

标签: swift cocoa authorization sudo cfpreferences

当前正在尝试修改/Library/Preferences/com.apple.Bluetooth.plist,以便以编程方式删除Bluetooth设备并出现错误。

首先,当调用SCPreferencesSetValue时,系统配置会显示

  

错误Domain = com.apple.SystemConfiguration代码= 0“成功!” UserInfo = {NSDescription =成功!}

但是,当我尝试SCPreferencesCommitChanges时,框架会告诉我

  

错误Domain = com.apple.SystemConfiguration代码= 1002“无效的参数” UserInfo = {NSDescription =无效的参数}

我不知道如何成功设置值,但是使用常规首选项对象提交值失败...任何帮助将不胜感激。谢谢!


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())")
}

0 个答案:

没有答案