Successfully stored String value to keychain, but always failed to read it out

时光毁灭记忆、已成空白 提交于 2019-12-22 13:46:46

问题


I am developing an iOS project with XCode8 + Swift3.

I have created the following two functions to store string to keychain and read it back from 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

}

I invoke the functions:

boolean hasStored = storeString(value: "John")
let readVal = readString()

I got hasStored is true, but readVal always nil. I investigated my functions, I see I always get error -50 as status code in reader function (see my comment in my function).

Why? Why I can't read the value I stored in keychain (I am not sure whether it is really stored but I do get status == noErr always retuns true in storeString(value:) function)


回答1:


There is a typo in your code:

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

kSecMatchLimit is the key and not a valid value. The expected value should be a CFNumber or kSecMatchLimitOne or kSecMatchLimitAll. If you expect a single item to be returned, use kSecMatchLimitOne. See also Search Attribute Keys and Values.



来源:https://stackoverflow.com/questions/45617160/successfully-stored-string-value-to-keychain-but-always-failed-to-read-it-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!