使用UIWebView忽略无效的服务器证书 [英] Ignoring Invalid Server Certificates with UIWebView

查看:685
本文介绍了使用UIWebView忽略无效的服务器证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have an iOS app that uses a UIWebView to display content. We load it up with data with code that looks like this:

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

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

This used to work fine with HTTP requests, but now we are using HTTPS against a server with a self-signed SSL certificate. When the above is run, the webView:didFailLoadWithError: delegate method gets called, with this error:


此服务器的证书无效。您可能连接到假装是blah.blah.blah.com的服务器,这可能会使您的机密信息处于危险之中。

The certificate for this server is invalid. You might be connecting to a server that is pretending to be "blah.blah.blah.com" which could put your confidential information at risk."

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

I would like to simply ignore the invalid certificate and go on with the request, as one can do in Mobile Safari.

我已经看过如何解决使用 NSURLConnection 时出现此问题(请参阅例如,旧的iphone 3g上的HTTPS请求,但是用 UIWebView 可以做什么?

I have seen how to work around this issue when using NSURLConnection (see HTTPS request on old iphone 3g, for example), but what can one do with a UIWebView?

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

I imagine that I could rework the code so that it uses NSURLConnection to make the requests and then puts the results into the web view by calling its loadHTMLString:baseURL: method, but that's going to get complicated when the pages have images, CSS, JavaScript, and so on. Is there an easier way?

推荐答案

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

Please note: This API is currently unsupported, and should really only be used in a safe testing environment. For further details, take a look at this CocoaNetics article.

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

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; will allow you to ignore certificate errors. You will also need to add the following to the beginning of your file to grant you access to these private APIs:

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

这篇关于使用UIWebView忽略无效的服务器证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆