我正在使用XCode8 + Swift3开发一个iOS项目。
我创建了以下两个函数来将字符串存储到keychain并从keychain读回:
var query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: "my service",
kSecAttrAccount as String: "my-key"
]
func storeString(value: String) -> Bool {
if let data = value.data(using: .utf8) {
// delete data if exist
SecItemDelete(query as CFDictionary)
// add value to query
query[kSecValueData as String] = data
// add to keychain
let status = SecItemAdd(query as CFDictionary, nil)
return status == noErr
}
return false
}
func readString() -> String? {
// update query
query[kSecReturnData as String] = kCFBooleanTrue
query[kSecMatchLimit as String] = kSecMatchLimit
var result: AnyObject?
// fetch items from keychain
let status: OSStatus = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
}
// I always get error -50 here
if status == noErr {
if let resultData = result as? Data {
if let strVal = String(data: resultData, encoding: .utf8) {
return strVal
}
}
}
return nil
}
我调用函数:
boolean hasStored = storeString(value: "John")
let readVal = readString()
我得到hasStored
是true
,但readVal
总是nil
。我调查了我的函数,我发现我总是得到错误-50作为读者函数中的状态代码(参见我在函数中的评论)。
为什么?为什么我无法读取存储在钥匙串中的值(我不确定它是否真的存储但我确实得到status == noErr
总是在true
函数中重新调整storeString(value:)
)
你的代码中有一个拼写错误:
query[kSecMatchLimit as String] = kSecMatchLimit
// ^~~~~~~~~~~~~~
kSecMatchLimit
是关键而非有效价值。预期值应为CFNumber
或kSecMatchLimitOne
或kSecMatchLimitAll
。如果您希望返回单个项目,请使用kSecMatchLimitOne
。另见Search Attribute Keys and Values。