pierre23
pierre23

Reputation: 3936

NSURLErrorDomain -1202 on iPhone 4 only

My application, currently on Apple Store has a login feature. I received a lot complain from customer with iPhone 4 only. They are complaining that they cannot log into their account. This happen only on iPhone 4 devices (maybe holder devices too). The error returns me a code -1202 (NSURLErrorServerCertificateUntrusted). What I don't understand is that it works on any newer devices (iPhone 4S, 5, 5C and 5S).

- (IBAction)didTapButton:(id)sender 
{
   NSURL *url = [NSURL URLWithString:@"MY_LOGIN_URL"];
   NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  [connection start];
}

#pragma mark - NSURLConnectionDataDelegate

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
  NSLog(@"Failed");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  NSLog(@"Succeded");
}

I know that I can force the app to trust the server by implanting the lines below but this is not a valid solution.

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
  [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
 {
  return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

Is it enough if I add in didReceiveAuthenticationChallenge:

SecTrustRef trustRef = [[challenge protectionSpace] serverTrust];
SecTrustEvaluate(trustRef, NULL);
CFIndex count = SecTrustGetCertificateCount(trustRef);
if(count > 0)
{
  SecCertificateRef certRef = SecTrustGetCertificateAtIndex(trustRef, 0);
  CFStringRef certSummary = SecCertificateCopySubjectSummary(certRef);
  NSString* certSummaryNs = (__bridge NSString*)certSummary;
  if([certSummaryNs isEqualToString:@"MY_API"])
    NSLog(@"Verified");
  else
    NSLog(@"Invalid");
}
else
  NSLog(@"No certificate found");

Upvotes: 0

Views: 792

Answers (1)

Sachin Siwal
Sachin Siwal

Reputation: 311

Did you try device setting that need to change the date and time , and set it automatically ON for current time zone .

Upvotes: 0

Related Questions