I use the following code to retrieve the login credentials from the iPhone keychain:
KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Test" accessGroup:nil];
NSString *username = [wrapper objectForKey:(id)kSecAttrAccount];
NSString *password = [wrapper objectForKey:(id)kSecValueData];
[wrapper release];
I'm under the impression that the first time a user launches the app, neither username nor password could be retrieved from the keychain, so username
and password
should be equal to nil
. I, however, was unable to print out any of these variables using NSLog
.
Any suggestions?
KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Test" accessGroup:nil];
NSString *username = [wrapper objectForKey:(id)kSecAttrAccount];
NSString *password = [wrapper objectForKey:(id)kSecValueData];
// initially all these are empty
NSLog(@"username - %@", username); // username -
NSLog(@"password - %@", password); // password -
//if empty set your credentials
if ( [username isEqualToString:@""] ) {
[wrapper setObject:@"your username here" forKey:(id)kSecAttrAccount];
}
if ( [password isEqualToString:@""] ) {
[wrapper setObject:@"your password here" forKey:(id)kSecValueData];
}
//get the latest credentials - now you have the set values
username = [wrapper objectForKey:(id)kSecAttrAccount];
password = [wrapper objectForKey:(id)kSecValueData];
NSLog(@"username - %@", username); // username - your username here
NSLog(@"password - %@", password); // password - your password here
// reset your keychain items - if needed
[wrapper resetKeychainItem];
[wrapper release];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With