iOS save multiple passwords in keychain
Asked Answered
T

1

6

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?

Torrence answered 15/6, 2012 at 17:46 Comment(2)
[[KeychainItemWrapper alloc] id1 accessGroup:nil]; <== how does this even compile?Garrard
Sorry, post wast not made by copy&paste but out of my mind! I did the editing...Torrence
T
9

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

Torrence answered 15/6, 2012 at 18:39 Comment(1)
I've found the KeychainItemWrapper has various small holes like this. After using it and modifying it for multiple projects I created a much simpler wrapper around accessing the Keychain github.com/reidmain/FDKeychainRepentant

© 2022 - 2024 — McMap. All rights reserved.