Search code examples
iphoneiospassword-protectionkeychain

IOS store more than one password in keychain


in my app I can save an read fine 1 password store in the keychain using this code

// save password
[keychainItem setObject:textFieldPassword.text forKey:(__bridge id)(kSecValueData)];

//get pasword from keychain
NSString *_password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];

My question is: how can I store more than 1 password at a time in the keychain?


Solution

  • Thank you all for your answers.

    Here the solution I used:

    adding to my project the files KeychainItemWrapper.h/m

    allocating 2 keychain items:

    //aloc for user password
    keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"passowrdKey1" accessGroup:nil];
    //aloc for user password2
    keychainItem2 = [[KeychainItemWrapper alloc] initWithIdentifier:@"passowrdKey1" accessGroup:nil];
    

    then just use this to read/write

    //WRITE    
    // save password
    [keychainItem setObject:@"password1" forKey:(__bridge id)(kSecValueData)];
    // save password2
    [keychainItem2 setObject:@"password2" forKey:(__bridge id)(kSecValueData)];
    
    //READ        
    //get pasword from keychain
    NSString *_pass = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
    
    //get pasword from keychain
    NSString *_pass2 = [keychainItem2 objectForKey:(__bridge id)(kSecValueData)];