使用 Locksmith 在钥匙串中存储多个值

Storing multiple values in Keychain using Locksmith

你能在一个 forUserAccount 下存储多个 key/value 对吗?

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 必须是唯一的,那它有什么用呢?那样的话,似乎就没有必要了。

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 应该是唯一的,可能它覆盖了以前的值。

就像@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")