Reputation: 137
I'm trying to use the KeychainItemWrapper.h
and keychainWrapperItemWrapper.m
to store user credentials such as username and password. I currently store when user logs in for the first time like this:
KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"login" accessGroup:nil];
[keychain setObject:_usernameField.text forKey:(__bridge id)kSecValueData];
[keychain setObject:_passwordField.text forKey:(__bridge id)kSecAttrAccount];
This stores the values in keychain. But the next time user opens app I want to retrieve the username and password again. However when i call the following:
NSString *password_ = [keychain objectForKey:(__bridge id)kSecValueData];
NSString *username_ = [keychain objectForKey:(__bridge id)kSecAttrAccount];
It seems i get weird encrypted key such as: <6f78696c 69676874 2d746573 74>
Is there any way to retrieve the original strings for username and password?
I've never worked with keychain before, any help would be appreciated!
Upvotes: 1
Views: 1816
Reputation: 2309
Why don't use this https://redflowerinc.com/keychain-wrapper-in-objective-c/
NS_ASSUME_NONNULL_BEGIN
@interface KeyChainWrapper : NSObject
+(NSMutableDictionary*) queryDictionary:(NSString*) identifier;
+(NSString*) searchKeychainCopyMatching:(NSString*) identifier;
+(bool) createKeychainValue:(NSString*) value forIdentifier:(NSString*) identifier;
+(bool) updateKeychainValue:(NSString*) password forIdentifier:(NSString*) identifier;
+(void) deleteKeychainValue:(NSString*) identifier;
@end
NS_ASSUME_NONNULL_END
Upvotes: 0
Reputation: 1515
If you use the wrapper linked above, https://koenig-media.raywenderlich.com/uploads/2014/12/KeychainWrapper.zip. You can set username and password like this. I renamed the methods for myObjectForKey and mySetObject. You can initialize
self.keychainWrapper = [[KeychainWrapper alloc] init];
in your init method.
//save user and password
[self.keychainWrapper setObject:username forKey:(__bridge id)kSecAttrAccount];
[self.keychainWrapper setObject:_password forKey:(__bridgeid)kSecValueData];
[self.keychainWrapper writeToKeychain];
//retrieve username and password
NSString *username = [self.keychainWrapper objectForKey:(__bridge id)kSecAttrAccount];
NSString *password = [self.keychainWrapper objectForKey:(__bridge id)kSecValueData];
Upvotes: 1