UIWebView加载自签名Https网页
2016-11-10 14:36
225 查看
ATS设置
按照惯例写一个UIWebView,用来加载网页:_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; _webView.delegate = self; [self.view addSubview:_webView]; NSURL *url = [NSURL URLWithString:@"https://github.com/"]; _request = [NSURLRequest requestWithURL:url]; [_webView loadRequest:_request];
run一下看看加载出来了吗?如果发现屏幕一片白并没有出现网页内容,不要惊讶,看看你的控制台是不是报出以下错误:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
这个怎么解决呢?没错ATS设置:去plist文件里添加一项App Transport Security Settings,它是个字典类型,给它增加一对键值,键:Allow Arbitrary Loads ,值设为YES。
以上,搞定ATS设置,网页成功加载了:
Simulator Screen Shot.png
如果你的网页是self signed website,那么你的屏幕应该还是一片白,并且控制台又报错:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
注意:这两次错误并不是一样的,后面数字代码不同
(并不太清楚这个码代表的意思,有知道的朋友请留言告知,感谢。)
NSURLConnection
使用webview加载自签名https站点的时候,必须在请求的时候将该站点设置为安全的,才能继续访问。所以我们需要在webview开始加载网页的时候首先判断判断该站点是不是https站点,如果是的话,先让他暂停加载,用NSURLConnection 来访问改站点,然后再身份验证的时候,将该站点置为可信任站点。然后在用webview重新加载请求。直接上代码:
#pragma mark - UIWebViewDelegate // Note: This method is particularly important. As the server is using a self signed certificate, // we cannot use just UIWebView - as it doesn't allow for using self-certs. Instead, we stop the // request in this method below, create an NSURLConnection (which can allow self-certs via the delegate methods // which UIWebView does not have), authenticate using NSURLConnection, then use another UIWebView to complete // the loading and viewing of the page. See connection:didReceiveAuthenticationChallenge to see how this works. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; { NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated); if (!_authenticated) { _authenticated = NO; _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self]; [_urlConnection start]; return NO; } return YES; } #pragma mark - NURLConnection delegate - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; { NSLog(@"WebController Got auth challange via NSURLConnection"); if ([challenge previousFailureCount] == 0) { _authenticated = YES; NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; } else { [[challenge sender] cancelAuthenticationChallenge:challenge]; } } // We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } #pragma mark - NSURLConnectionDataDelegate - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; { NSLog(@"WebController received response via NSURLConnection"); // remake a webview call now that authentication has passed ok. _authenticated = YES; [_web loadRequest:_request]; // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!) [_urlConnection cancel]; }
以上设置,可成功加载自签名网页。
但是,虽然加载成功,但是控制台还是报了以下错误:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
文/Goyakod(简书作者)
原文链接:http://www.jianshu.com/p/91eb1d8817f2
参考链接:http://blog.csdn.net/yi_zz32/article/details/50146717
相关文章推荐
- iOS UIWebView 加载自签名证书Https网页
- Android webview 加载自签名的https网页显示空白
- iOS之支持https与ssl双向验证(包含:解决UIWebView加载不了https网页的图片,css,js等外部资源)
- iOS之支持https与ssl双向验证(包含:解决UIWebView加载不了https网页的图片,css,js等外部资源)
- UIWebView加载https网页
- cocos2d-x 使用UIWebView加载网页(顺便可以看到如何用OC调C++)
- iOS:用UIWebView加载网页,退出控制器内存不减,每次加载持续增涨(内存警告)
- Iphone UIWebView加载网页开发
- 15.Xcode7中使用UIWebView时加载不出网页的问题
- Android webview加载https网页时图片无法加载显示问题
- UIWebView 加载网页时使用程序中的背景
- UIWebView——>一个简单的网页加载
- 使用UIWebView加载本地或远程服务器上的网页
- iOS UIWebView 加载网页、文件、 html
- https网页加载http资源提示加载不安全脚本
- UIWebView 加载网页、文件、 html-b
- ios UIWebView 加载网页、文件、 html
- Swift-01 UIWebView加载网页
- Android之WebView——解决加载Https以及根据网页动态设置WebView高度等难题
- android_https踩坑(2)android自签名证书Glide加载不出图片