iOS save multiple passwords in keychain

牧云@^-^@ 提交于 2019-12-21 05:00:16

问题


I want to save two passwords (one app pin and one backend pin) in my keychain and am wondering how this should work.
I am using the KeychainItemWrapper with to different identifiers.

KeychainItemWrapper *kcw1 = [[KeychainItemWrapper alloc] initWithIdentifier:id1 accessGroup:nil];
KeychainItemWrapper *kcw2 = [[KeychainItemWrapper alloc] initWithIdentifier:id2 accessGroup:nil];

and I am using either one to save and retrive the app or the backend pin.

I use :

[self.kcw1 setObject:aVerifyCode forKey:(__bridge id) kSecValueData]
[self.kcw2 setObject:aAppPin forKey:(__bridge id) kSecValueData]

But it doesn't work - result is -25299 - The specified item already exists in the keychain

So how can I save multiple passwords in my keychain?


回答1:


Okay I think I know what the problem is! KeychainItemWrapper uses the identifier for the kSecAttrGeneric.
But this is not the one to distinguish entries. If you want to save two accounts or two passwords in

kSecValueData

this will result in an arror (-25299) for duplicate entires.
The reason is, that the apple keychain api uses

kSecAttrAccount

and

kSecAttrService

to distinguish entries.
So you can modify the keychainItemWraper if you want like shown in this thread



来源:https://stackoverflow.com/questions/11055731/ios-save-multiple-passwords-in-keychain

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