views:

49

answers:

1

Hi,

I am trying to access a web service using NSURConnection, but i am not sure how to send password there because the example code given in php is setting CURLOPT_USERPWD field of libcurl, and there seems to be no such field in NSURLConnection/NSURLReqest.

+1  A: 

Take the username and password, and concat them together with a colon. Take the resulting string and Base64 encode it. Take the Base64 encoded string, and prepend "Basic " (with the space). Now take that string (Basic [Base64 encoded value]) and set it as the "Authorization" header for your request.

Alternatively, if you're using the NSURLConnection delegate methods, you can implement one of them with something like:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  NSURLCredential * credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];
  [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}

(warning: untested, typed in a browser. caveat implementor)

Dave DeLong
You mean this way: [request setValue:pwd forHTTPHeaderField:@"Authorization"];
itsaboutcode
@itsaboutcode yep, that's the one
Dave DeLong