Swift + Locksmith: Not getting stored Keychain Value
Asked Answered
F

1

0

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?

Frameup answered 5/7, 2016 at 20:4 Comment(1)
Are you running the simulator app?Kipper
N
3

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 {
Ninnyhammer answered 5/7, 2016 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.