Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

johnmerlino

macrumors member
Original poster
Oct 22, 2011
81
0
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];
}
 

johnmerlino

macrumors member
Original poster
Oct 22, 2011
81
0
Turned off iphone and turned it back on and then it works. Another example of poorly designed infrastructure. Thank goodness for android.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.