仅限iPhone 4上的NSURLErrorDomain -1202

时间:2014-09-17 18:20:37

标签: ios objective-c nsurlerrordomain

我的应用程序,目前在Apple Store上有一个登录功能。我收到了很多客户的抱怨,仅限iPhone 4。他们抱怨他们无法登录他们的帐户。这只发生在iPhone 4设备上(也可能是支架设备)。该错误返回代码-1202(NSURLErrorServerCertificateUntrusted)。我不明白的是它适用于任何新设备(iPhone 4S,5,5C和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");
}

我知道我可以强制应用程序通过植入下面的行来信任服务器,但这不是一个有效的解决方案。

- (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];
}

如果我添加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");

1 个答案:

答案 0 :(得分:0)

您尝试过更改日期和时间的设备设置,并将其自动设置为当前时区吗。