NSURLConnection and Basic HTTP Authentication in iOS
I'm using an asynchronous connection with MGTwitterEngine and it sets the authorization in the NSMutableURLRequest
(theRequest
) like so:
NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self username], [self password]];NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]];[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
I don't believe this method requires going through the challenge loop but I could be wrong
Even the question is answered, I want to present the solution, which doesn't require external libs, I found in another thread:
// Setup NSURLConnectionNSURL *URL = [NSURL URLWithString:url];NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];[connection start];[connection release];// NSURLConnection Delegates- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if ([challenge previousFailureCount] == 0) { NSLog(@"received authentication challenge"); NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER" password:@"PASSWORD" persistence:NSURLCredentialPersistenceForSession]; NSLog(@"credential created"); [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; NSLog(@"responded to authentication challenge"); } else { NSLog(@"previous authentication failure"); }}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { ...}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { ...}- (void)connectionDidFinishLoading:(NSURLConnection *)connection { ...}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { ...}
Here is a detailed answer with no 3rd party involved:
Please check here:
//username and password valueNSString *username = @“your_username”;NSString *password = @“your_password”;//HTTP Basic AuthenticationNSString *authenticationString = [NSString stringWithFormat:@"%@:%@", username, password]];NSData *authenticationData = [authenticationString dataUsingEncoding:NSASCIIStringEncoding];NSString *authenticationValue = [authenticationData base64Encoding];//Set up your requestNSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.your-api.com/“]];// Set your user login credentials[request setValue:[NSString stringWithFormat:@"Basic %@", authenticationValue] forHTTPHeaderField:@"Authorization"];// Send your request asynchronously[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *responseCode, NSData *responseData, NSError *responseError) { if ([responseData length] > 0 && responseError == nil){ //logic here }else if ([responseData length] == 0 && responseError == nil){ NSLog(@"data error: %@", responseError); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error accessing the data" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil]; [alert show]; [alert release]; }else if (responseError != nil && responseError.code == NSURLErrorTimedOut){ NSLog(@"data timeout: %@”, NSURLErrorTimedOut); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"connection timeout" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil]; [alert show]; [alert release]; }else if (responseError != nil){ NSLog(@"data download error: %@”,responseError); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"data download error" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil]; [alert show]; [alert release]; }}]
Kindly let me know your feedback on this.
Thanks