【发布时间】:2014-11-11 21:49:36
【问题描述】:
我的应用程序,目前在 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");
【问题讨论】:
-
哪些 iOS 版本有问题?
-
是的,我认为可能在旧版本的 IOS 中,信任存储会过于持有,因此会使请求失败。但我目前正在使用最新版本的 IOS 7.1 进行测试,我遇到了同样的问题。
-
@user1735977 你应该试试这个cocoanetics.com/2009/11/… 如果不起作用,请检查你的 iPhone4 设备中的设置时间,有时日期时间不正确,也会产生 NSURLErrorServerCertificateUntrusted 错误。问候
-
好的,我找到了问题。这不是IOS问题。服务器上缺少一些证书......仍然无法解释为什么它可以在 iPhone 4 以外的其他设备上运行。但现在它可以工作了。谢谢你们的帮助
-
嗨 pierre23,你能提供解决这个问题的方法吗,因为我在 iPhone 4 中也面临同样的问题,但没有找到任何解决方案。
标签: ios objective-c nsurlerrordomain