title: iphone: secure restfull server "The certificate for this server is invalid
body: <p>I am trying to consume secure restful service which gives error</p>
  
  <p><strong>Error = Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xxx.xxx.xxx.xxx” which could put your confidential information at risk."</strong></p>
  
  <p>working on xCode 4.2, where is the mistake or any step missing.</p>
  
  <p>using following code</p>
  
  <p>RegisterUser.f</p>
  
  <pre><code>@interface RegisterUser : UIViewController&lt;UITextFieldDelegate,
  UIScrollViewDelegate, NSURLConnectionDelegate&gt;
  </code></pre>
  
  <p>RegisterUser.m</p>
  
  <pre><code>- (IBAction)SubmitBtnAction:(id)sender {
  
      NSURL *url = [NSURL URLWithString:@"https://xx.xx.xx.xxx:8223/jaxrs/tunedoorgateway/getCountries"];
      NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
  
      [NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init]
       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
       {
  
           if ([data length] &gt;0 &amp;&amp; error == nil)
           {
               NSLog(@"Data = %@", data);
               // DO YOUR WORK HERE
  
           }
           else if ([data length] == 0 &amp;&amp; error == nil)
           {
               NSLog(@"Nothing was downloaded.");
           }
           else if (error != nil){
               NSLog(@"Error = %@", error);
           }
  
       }];
  
  }
  
  
  - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
      NSLog(@"This is canAuthenticateAgainstProtectionSpace");
      return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
  }
  
  
  - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  {
  //    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
  //        if ([trustedHosts containsObject:challenge.protectionSpace.host])
              [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
  
      [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
      NSLog(@"This is didReceiveAuthenticationChallenge");
    //  [[challenge sender] cancelAuthenticationChallenge:challenge];
  }
  </code></pre>