为什么有些Https url没有加载到WebView中?

时间:2012-05-28 10:02:16

标签: iphone ios xcode cocoa-touch uiwebview

为什么某些https链接未加载WebView。我可以看到一些https url在safari浏览器中完美加载,但是当我尝试在webview中加载相同的url时,它没有加载。可能是因为https有自签名证书?我们不能在具有自签名证书的WebView中加载URL吗?

编辑: 现在我可以打电话了

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

仍然没有在WebView中加载我的https网址。

1 个答案:

答案 0 :(得分:0)

试试这个:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
  return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSArray *trustedHosts = [NSArray arrayWithObjects:@"mytrustedhost",nil];

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