我遇到以下问题:
我的应用有一个UIWebView
,在此web view
内,我想访问受website
保护的SSL
。 ssl certificate
是自签名的
我已经实现了以下方法:
shouldStartLoadWithRequest:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
if(![self authenticated])
{
[self setAuthenticated:NO];
[self setUrlConnection:[[NSURLConnection alloc] initWithRequest:[self requestObj] delegate:self]];
[[self urlConnection] start];
return NO;
}
return YES;
}
didReceiveAuthenticationChallenge:
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if([challenge previousFailureCount] == 0)
{
[self setAuthenticated:YES];
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
}
else [[challenge sender] cancelAuthenticationChallenge:challenge];
}
didReceiveResponse:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
[self setAuthenticated:YES];
[[self webView] loadRequest:[self requestObj]];
[[self urlConnection] cancel];
}
canAuthenticateAgainstProtectionSpace:
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
我忘记了某些功能或一行代码吗?如果我访问该网站,我收到了错误
2016-05-03 13:46:35.819 App[2121:806751] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
2016-05-03 13:46:35.965 App[2121:806745] CFNetwork SSLHandshake failed (-9824 -> -9829)
2016-05-03 13:46:35.967 App[2121:806745] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9829)
网站应该显示我从服务器获得的错误几秒钟,然后加载网站。
我还在Info.plist
中添加了以下几行:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
服务器配置正确(https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html?hl=fi)“使用ATS连接的要求”