成功将String值存储到keychain,但始终无法读取它

问题描述 投票:2回答:1

我正在使用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()

我得到hasStoredtrue,但readVal总是nil。我调查了我的函数,我发现我总是得到错误-50作为读者函数中的状态代码(参见我在函数中的评论)。

为什么?为什么我无法读取存储在钥匙串中的值(我不确定它是否真的存储但我确实得到status == noErr总是在true函数中重新调整storeString(value:)

ios swift3 keychain
1个回答
5
投票

你的代码中有一个拼写错误:

query[kSecMatchLimit as String] = kSecMatchLimit
//                                ^~~~~~~~~~~~~~

kSecMatchLimit是关键而非有效价值。预期值应为CFNumberkSecMatchLimitOnekSecMatchLimitAll。如果您希望返回单个项目,请使用kSecMatchLimitOne。另见Search Attribute Keys and Values

© www.soinside.com 2019 - 2024. All rights reserved.