While testing device, I expect when I clicking run if I loaded information in keychain already, then it should exist when I run it again. However, the username and pass stored in keychain does not exist on a second run. Maybe because its instantiating a new instance of keychain during each viewDidLoad, but then how else would I get the existing keychain information?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"XKRemoteiPhone" accessGroup:nil];
}
- (IBAction)performLogin:(id)sender {
NSString* username;
NSString* password;
if (![[keychain objectForKey:(__bridge id)kSecAttrAccount] length] || ![[keychain objectForKey:(__bridge id)kSecValueData] length]){
username = self.emailField.text;
password = self.passwordField.text;
[keychain setObject:self.emailField.text forKey:(__bridge id)kSecAttrAccount];
[keychain setObject:self.passwordField.text forKey:(__bridge id)kSecValueData];
}
else {
username = [keychain objectForKey:(__bridge id)kSecAttrAccount];
password = [keychain objectForKey:(__bridge id)kSecValueData];
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/pages/login.json", RemoteUrl]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request setRequestMethod:@"POST"];
[request appendPostData:[[NSString stringWithFormat:@"{\"user\":{\"email\":\"%@\", \"password\":\"%@\"}}", username, password] dataUsingEncoding:NSUTF8StringEncoding] ];
[request setNumberOfTimesToRetryOnTimeout:5];
[request setTimeOutSeconds:100];
[request startSynchronous];
}