Basic HTTP Authentication on iPhone

I am trying to start a small Twitter client and I encountered a problem while testing API calls requiring authentication.

My password has special characters, so when I try to use the following code, it does not work.

NSString *post = [NSString stringWithFormat:@"status=%@", [status stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@@%@/statuses/update.json", username, password, TwitterHostname]]; [request setURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLResponse *response; NSError *error; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

I started learning base64 and put authentication in the headers. I found Dave Dribin's post about my base64 implementation, and that seemed to make sense. However, when I tried to use it, the compiler started complaining about how it could not find the openssl libraries. So I read that I need to link the libcrypto library, but it does not seem to exist for the iphone.

I also read people saying that an apple will not allow applications that use cryptographic libraries, it makes no sense to me.

So now I'm a little stuck and embarrassed. What is the easiest way to get basic authentication in my application?

Greetings

+13
Jun 14 '09 at 18:23
source share
2 answers

Two things. First, you should use async methods, not the synchronous / class method.

 NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:req] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; // create the connection with the request // and start loading the data NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

Authentication management is carried out by implementing this method in your deletet:

 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; 

And you will also probably have to implement these methods:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; - (void)connectionDidFinishLoading:(NSURLConnection *)connection; 

Using the async method tends to give a better user experience anyway, so even though the added complexity is worth doing even without the possibility of authentication.

+15
Jun 14 '09 at 18:27
source

You can directly write an email with a username and password in the main URL ie https: // username: password@yoururl.com/

First of all you need to call the delegate file NSURLConnection: -

  • (BOOL) connection: (NSURLConnection *) connection canAuthenticateAgainstProtectionSpace: (NSURLProtectionSpace *) protectionSpace

    {

    return YES; }

and then call - (void) connection: (NSURLConnection *) connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *) call

 { if ([challenge previousFailureCount] == 0) { NSURLCredential *newCredential; newCredential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession]; NSLog(@"NEwCred:- %@ %@", newCredential, newCredential); [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; } else { [[challenge sender] cancelAuthenticationChallenge:challenge]; NSLog (@"failed authentication"); } } 
0
Jan 28 '14 at 5:38
source



All Articles