Question

I am using SSKeychain for storing my credentials in iphone. but when I tried to retrieve it by using the below code

NSArray *accountsArr = [SSKeychain accountsForService:@"User"];
    if(accountsArr)
    {
            NSDictionary *credentialsDict =accountsArr[0];
            NSArray *userNames = [credentialsDict allKeys];
            NSString *credential = (NSString *)credentialsDict[userNames[3]];
            self.usernameTxtFld.text = credential;
            credential = [SSKeychain passwordForService:@"User" account:self.usernameTxtFld.text];
            self.passwordTxTFld.text =  credential.length>0?credential:nil;
    }

I need to specify userNames[3] for the stored username does this appears always in index 3 or should I use another method for this??

Was it helpful?

Solution

No, because dictionary keys have no inherent order. The NSDictionary documentation makes this clear in its discussion of the allKeys method:

The order of the elements in the array is not defined.

The point of having a dictionary is that you look up something by a specific key. In this case, it appears you should get the account name using the kSSKeychainAccountKey constant defined in SSKeychain.h, as shown:

NSArray *accountsArray = [SSKeychain accountsForService:@"User"];
if ([accountsArray count] > 0) {
    NSDictionary *credentialsDictionary = accountsArray[0];
    NSString *accountName = credentialsDictionary[kSSKeychainAccountKey];
    NSString *password = [SSKeychain passwordForService:@"User" account:accountName];
    self.passwordTextField.text = password;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top