Swift + Locksmith: Not getting stored Keychain Value

℡╲_俬逩灬. 提交于 2020-01-07 00:36:59

问题


So I seem to be having some issues getting this value from the Keychain.

This is what I am using to save the password. This is called on the second view controller.

do {
   try Locksmith.updateData(["password": "Boom123456"], forUserAccount: "KeychainDemo")
   } catch {
    print("Unable to set password")
}

When I go back to the first view controller that has the check for keychain item on:

 let dictionary = Locksmith.loadDataForUserAccount("KeychainDemo")

    if let passwordSaved = dictionary!["password"]?.stringValue {

        print("password: \(passwordSaved)")


    } else {
        print("No Password")
    }

When I run the app I get:

No Password

in the console. However, When I set break points on the let dictionary and then type po dictionary in the console I get this output:

Optional <Dictionary<String, AnyObject>>
Some : 1 elements
   [0] : 2 elements
      - .0 : "password"
      - .1 : Boom123456

So it is there, Can anyone see why my code is not getting the password from the Keychain?


回答1:


Looks like you're pulling your "password" string out of the dictionary incorrectly. Don't use the .stringValue function in this case. Change this line:

if let passwordSaved = dictionary!["password"]?.stringValue {

to

if let passwordSaved = dictionary!["password"] as? String {


来源:https://stackoverflow.com/questions/38211806/swift-locksmith-not-getting-stored-keychain-value

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