Swift + Locksmith: Not getting stored Keychain Val

2019-09-11 00:08发布

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条回答
孤傲高冷的网名
2楼-- · 2019-09-11 00:53

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 {
查看更多
登录 后发表回答