有没有人find一种方法来加载HTTPS页面与无效的服务器证书使用UIWebView?

如果用户尝试在Mobile Safari中加载https网页并且服务器的证书validation检查失败(其过期,撤销,自签名等),则向用户呈现警告消息,并询问他们是否想要继续或不。

同样,NSURLConnection提供了执行者首先决定如何检查证书然后决定如何进行的能力,因此在这种情况下也可能向用户显示警告,并为他们提供继续加载的机会或不是。

但是,似乎在UIWebView中加载https证书失败证书检查行为只是无法加载页面 – didFailLoadWithError:被调用与kCFURLErrorServerCertificateUntrusted但是没有得到显示给用户。

这是不一致的 – 当然,UIWebView的行为应该像Safari一样,在iPhone本身内部保持一致? 它也是一个愚蠢的NSURLConnection允许这个NSURLRequest完全的灵活性:setAllowsAnyHTTPSCertificate是私人的。

是否有实现与Safari一致的行为,这种默认行为是否可以通过类似NSURLConnection允许的方式进行定制?

干杯

PS请不要进入光顾边讨论为什么有人想这样做,非常感谢。

我发现如何做到这一点:

1)当页面被加载时,将会失败,因此添加如下内容到didFailLoadWithError:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error if ([error.domain isEqualToString: NSURLErrorDomain]) { if (error.code == kCFURLErrorServerCertificateHasBadDate || error.code == kCFURLErrorServerCertificateUntrusted || error.code == kCFURLErrorServerCertificateHasUnknownRoot || error.code == kCFURLErrorServerCertificateNotYetValid) { display dialog to user telling them what happened and if they want to proceed 

2)如果用户想要加载页面,则需要使用NSURLConnection进行连接:

 NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.currentURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]; self.loadingUnvalidatedHTTPSPage = YES; [self.webView loadRequest:requestObj]; 

3)然后使这个更改为shouldStartLoadWithRequest

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if (self.loadingUnvalidatedHTTPSPage) { self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [self.connection start]; return NO; } 

4)实现NSURLConnectionDelegate为:

 - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { SecTrustRef trust = challenge.protectionSpace.serverTrust; NSURLCredential *cred; cred = [NSURLCredential credentialForTrust:trust]; [challenge.sender useCredential:cred forAuthenticationChallenge:challenge]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; { NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.currentURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]; self.loadingUnvalidatedHTTPSPage = NO; [self.webView loadRequest: requestObj]; [self.connection cancel]; } 

这一切似乎工作正常。

从马的口中:

“UIWebView不提供任何方式让应用程序自定义其HTTPS服务器信任评估,可以使用公共API来解决这个限制,但这并不容易,如果你需要这样做,请联系开发者技术支持(dts) @ apple.com)

资料来源: http : //developer.apple.com/library/ios/#technotes/tn2232/_index.html