快速钥匙串添加,删除,开始工作,更新不工作

时间:2019-02-15 08:42:52

标签: swift macos keychain

我正在解决钥匙串更新问题。我有SecItemAdd,SecItemCopyMatching和SecItemDelete。但是我对SecItemUpdate感到困惑。我总是失败-50。

我在Github上放置了一个公共MacOS keychainTest项目,网址为: https://github.com/darrellroot/keychainTest/blob/master/README.md

仅供参考,这个几乎相同的问题/答案应该对我有帮助,但事实并非如此: Swift 4 Keychain SecItemUpdate Error status code -50

有关的代码段:

let keychainTest = "Keychain Test"
let serverHostname = "mail.nowhere.com"
let keychainService = "KeychainService"

AddQuery起作用:

let addQuery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: keychainService,
kSecAttrAccount as String: serverHostname,
kSecValueData as String: inputTextOutlet.stringValue]
let status = SecItemAdd(addQuery as CFDictionary, nil)

GetQuery起作用:

let getQuery: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: keychainService,
kSecAttrAccount as String: serverHostname,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnData as String: true]
var rawResult: AnyObject?
let status = SecItemCopyMatching(getQuery as CFDictionary, &rawResult)
statusLabel.stringValue = status.description        
guard let retrievedData = rawResult as? Data else { return }
guard let pass = String(data: retrievedData, encoding: String.Encoding.utf8) else { return }

DeleteQuery的工作原理:

let deleteQuery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
                                  kSecAttrService as String: keychainService,
                                  kSecAttrAccount as String: serverHostname]
let status = SecItemDelete(deleteQuery as CFDictionary)
statusLabel.stringValue = status.description

但是对于此updateQuery,我总是收到-50 OSStatus消息:

let updateQuery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
                                  kSecAttrService as String: keychainService,
                                  kSecAttrAccount as String: serverHostname]
 let newAttributes: [String: Any] = [kSecValueData as String: inputTextOutlet.stringValue]
 let status = SecItemUpdate(updateQuery as CFDictionary, newAttributes as CFDictionary)

在osstatus.com中,-50表示“传递给该函数的一个或多个参数无效。”

0 个答案:

没有答案