使用UIWebView忽略无效的服务器证书

时间:2011-07-22 15:21:05

标签: ios uiwebview

我们有一个iOS应用程序,它使用UIWebView来显示内容。我们使用如下代码加载数据:

NSURL *url = [NSURL URLWithString:myURLString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView setDelegate:self];
[_webView loadRequest:request];

这曾经适用于HTTP请求,但现在我们对具有自签名SSL证书的服务器使用HTTPS。运行上述操作时,将调用webView:didFailLoadWithError:委托方法,并显示以下错误:

  

此服务器的证书无效。您可能正在连接到假装是" blah.blah.blah.com"这可能会使您的机密信息面临风险。"

我想简单地忽略无效证书并继续处理请求,就像在Mobile Safari中一样。

我已经了解了在使用NSURLConnection时如何解决此问题(例如,请参阅HTTPS request on old iphone 3g),但使用UIWebView可以做些什么?

我想我可以重做代码,以便它使用NSURLConnection来发出请求,然后通过调用loadHTMLString:baseURL:方法将结果放入网络视图中,但这样做了当页面有图像,CSS,JavaScript等时变得复杂。有更简单的方法吗?

4 个答案:

答案 0 :(得分:21)

请注意:此API目前不受支持,实际上只能在安全的测试环境中使用。有关详细信息,请查看此CocoaNetics article

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];将允许您忽略证书错误。您还需要将以下内容添加到文件的开头,以授予您访问这些私有API的权限:

@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

答案 1 :(得分:16)

大家都知道...... APPLE不会接受上面隐藏接口的使用。他们寻求使用私有API,这不是一个可接受的解决方案。因此,请不要将上述解决方案作为解决方法的方式发布,因为尽管它有效,但它会在AppStore中拒绝您。这使得它毫无用处。

以下是忽略无效服务器证书的ACCEPTABLE方法。您需要使用NSURLConnection并手动加载网页数据,如下所示:

.
.
.
    //Create a URL object.
    url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestObj delegate:self];
    [connection start];
}

然后,在你的代表......

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

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

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
[resultData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    NSString *htmlString = [[NSString alloc] initWithBytes:[resultData bytes] length:[resultData length] encoding:NSUTF8StringEncoding];
    [webView loadHTMLString:htmlString baseURL:url];
}
@end

其中resultData是您之前实例化的NSMutableData,其中url和urlAddress都是您在其他位置实例化并填充的内容。

不幸的是,我目前还不知道如何让实际的UIWebView在没有有效证书的情况下直接加载页面。

你的,GC

答案 2 :(得分:3)

事实证明,一旦网站通过取消的NSURLConnection进行身份验证,UIWebView就可以向网站发出请求。 There is a complete explanation here.

答案 3 :(得分:0)

据我所知,只有UIWebView这是不可能的。据我了解,您需要使用NSURLConnection来处理所有HTTP / HTTPS mojo,然后通过UIWebView-loadHtmlString:baseURL:将结果提供给-loadData:MIMEType:textEncodingName:baseURL:

相关问题