title: NSURLCredential-credentialForTrust vs username:password
body: <p>I am new with iOS and https stuffs. I am a beginning software student. I want to write an app that extract the data (such as class number, announcements, etc) from my uni account and show me on my app.</p>
  
  <p>Normally, I go to my uni student page, login with my username and password, and those information showed up on the browser.</p>
  
  <p>I've tried using NSURLConnection and its delegate. please look at my code below. In the beginning, in the method connection:didReceviveAuthenticationChallenge, I created an NSURLCredential object by [NSURLCredential withUser:password:persistence]. And it was not working. Then I tried to do NSLog, then i just found out that the protectionSpace.authenticationMethod is NSURLAuthenticationMethodServerTrust.</p>
  
  <p>Then, I tried to read Apple's document and some googled sources. But, I can't really get it, and then I edited my code as you seen below (it is my final edition, and its still not working).</p>
  
  <p>The point that i don't really get is: I expect that the server should ask me for my username and password. Instead of that, it asked for credentialForTrust, which based on those documents I've read, they suggest me to evaluate the trust of NSURLConnection delegate against the server. However, the server never asked for username and password. so, how can the server know which account I am accessing.</p>
  
  <p>So, i think it should be some relationship between that certificate(trust) with username and password. I don't really understand how these things work.</p>
  
  <p>I think, my question might not be really clear or something, and it might be foolish. I accept this because I've never learned all these things.</p>
  
  <p>So, please someones explain me of how these things work. You can assume that I have some basic understanding of what is https, SSL, NSURLConnection, NSURLCredential, etc. and please guide me to the solution.</p>
  
  <p>I am appreciate for all your efforts. </p>
  
  <p>Below is my code, (it's not working).</p>
  
  <p>The NSLog() in connection:didReceiveAuthenticationChallenge, print out "NSURLAuthenticationMethodServerTrust"</p>
  
  <pre><code>- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
  {
      return YES;
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  {
      NSLog(@"authenticationMethod is:  %@\n",challenge.protectionSpace.authenticationMethod);
      if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
      }else{
              NSURLCredential *creden = [[NSURLCredential alloc] initWithUser:myusername password:mypassword persistence:NSURLCredentialPersistenceForSession];
              [[challenge sender] useCredential:creden forAuthenticationChallenge:challenge];
      }
  }
  
  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  {
      NSLog(@"didFailWithError:  %@\n  %@\n",
            [error localizedDescription],
            [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
       [self.myData appendData:data];
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  {
      [self.myData setLength:0];
  }
  
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  {
       NSString *str = [[NSString alloc] initWithData:self.myData    encoding:NSASCIIStringEncoding];
      NSLog(@"Data in String %@", str);
  }
  </code></pre>