我可以将物品添加到钥匙串中,甚至可以读出它们。
但是,我正在努力使用SecItemUpdate
更新值,并且似乎每次都返回errSecParam。
为此,我正在创建以下查询和属性(在这里很可能是个问题)
let query: [String: Any] = [kSecAttrAccount as String : "MyString",
kSecValueData as String: "test2".data(using: .utf16)!,
kSecMatchLimit as String : kSecMatchLimitOne
]
let attributes: [String: Any] = [kSecAttrAccount as String: "aa",
kSecValueData as String: data]
然后用于更新
SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
应该在查询中指定类而不是数据(用于更新),例如,要在钥匙串中添加/更新密码,应如下所示
// adding item
let addQuery: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: service as CFString, // eg. "www.mysite.com"
kSecAttrAccount: name as CFString // eg. "user"
kSecValueData: password.data(using: .utf8) // eg. "password"
] as CFDictionary
if errSecSuccess != SecItemAdd(addQuery, nil) {
// report error here
}
// updating item (same query is for SecItemDelete)
let updateQuery: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: service as CFString, // eg. "www.mysite.com"
kSecAttrAccount: name as CFString // eg. "user"
] as CFDictionary
let newAttributes = [
kSecAttrAccount: newName as CFString // eg. "user1"
kSecValueData: newPassword.data(using: .utf8) // eg. "password1"
] as CFDictionary
if errSecSuccess != SecItemUpdate(updateQuery, newAttributes) {
// report error here
}