1

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!

Saurabh Jain
  • 1,688
  • 14
  • 28
Abhishek Ravi
  • 137
  • 11
  • There's nothing obviously wrong with your code, although it looks like somewhere along the way an `NSString` is getting converted to `NSData`. The value `<6f78696c 69676874 2d746573 74>` is the `NSData` equivalent of the `NSString` "oxilight-test". Where did you get `KeychainItemWrapper.h` and how are you integrating it with your project? Are you using CocoaPods? Which version of Xcode are you using? What version of iOS are you using? – idz Aug 16 '17 at 23:12
  • 1
    Are you using this? - https://gist.github.com/dhoerl/1170641. This script have lots of issues with it. Try some other like - https://koenig-media.raywenderlich.com/uploads/2014/12/KeychainWrapper.zip | https://github.com/vsouza/awesome-ios#keychain – Lal Krishna Aug 17 '17 at 03:37

2 Answers2

1

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];
John Franke
  • 1,444
  • 19
  • 23
0

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
barryjones
  • 2,149
  • 1
  • 17
  • 21