使用Locksmith在Keychain中存储多个值

时间:2016-03-01 17:02:45

标签: swift keychain

您可以在一个forUserAccount下存储多个键/值对吗?

try? Locksmith.saveData(["access_token" : access_token], forUserAccount: "UserData")
try? Locksmith.saveData(["email" : email!], forUserAccount: "UserData")
try? Locksmith.saveData(["markets" : market], forUserAccount: "UserData")

此代码在查找"email."时失败。我能做到这一点的唯一方法是创建多个forUserAccount字符串:UserDataUserData1UserData3等等。我试过updateData但没有运气。如果forUserAccount必须是唯一的,它的用途是什么?如果是这样的话似乎没有必要。

2 个答案:

答案 0 :(得分:2)

let userIDNumber = userID as NSNumber
let userIDString : String = userIDNumber.stringValue
let keychainData = [_keychainEmailKey: email, _keychainPasswordKey: password, _keychainUserIDKey: userIDString]

Locksmith.updateData(keychainData, forUserAccount: email)

其中_keychainEmailKey等是键的常量。因此forUserAccount应该是唯一的,可能它会覆盖以前的值。

答案 1 :(得分:1)

就像@ nyekimov的答案所暗示的那样,可以为帐户存储值的字典。为了澄清,这是一段代码片段:

    do
    {
        try Locksmith.saveData(["user": "notMyRealUsername", "password":"123456"], forUserAccount: "myUserAccount") //store it
        let dictionary = Locksmith.loadDataForUserAccount("myUserAccount") //load it 
        print(dictionary) //let's see what we've got
    } 
    catch 
    {
    // handle errors here    
    }

帐户存在时使用try Locksmith.updateData(["user": "notMyRealUsernameEither", "password":"123456"], forUserAccount: "myUserAccount")