title: iPhone: How to Get Basic Authentication to HTTPS Web Service Using NSURLCredential
body: <p>I am trying to call an https web service (RESTful) using basic authentication.
  It works fine if I put the credentials in the url itself but I would rather add it to the request so that the password does not appear, for instance in an exception.</p>
  
  <p>I am using the following code:</p>
  
  <pre><code>    NSURLCredential *credential = [NSURLCredential credentialWithUser:@"myuser"
                                                               password:@"mypassword"
                                                            persistence:NSURLCredentialPersistenceForSession];
  
      NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                               initWithHost:@"example.com"
                                               port:443
                                               protocol:@"https"
                                               realm:nil
                                               authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
  
  
      [[NSURLCredentialStorage sharedCredentialStorage]  setDefaultCredential:credential
                                                          forProtectionSpace:protectionSpace];
  
      NSURLConnection *theConnection = [NSURLConnection  connectionWithRequest:theRequest delegate:self];
  </code></pre>
  
  <p>but it does not work. The <code>didReceiveAuthenticationChallenge</code> delegate method gets called and I can add a credential there but ideally I would send it with the request.</p>
  
  <p>Any ideas?</p>