UIWebView加载自签名Https网页
来源:互联网 发布:专柜mac口红多少钱 编辑:程序博客网 时间:2024/05/19 17:23
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设置,网页成功加载了:
如果你的网页是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
原文链接:http://www.jianshu.com/p/91eb1d8817f2
参考链接:http://blog.csdn.net/yi_zz32/article/details/50146717
0 0
- UIWebView加载自签名Https网页
- UIWebView加载https网页
- Android webview 加载自签名的https网页显示空白
- Glide加载自签名的https图片
- httpclient调用Https,加载自签名证书
- Android开发WebView加载自签名认证的https网址
- Glide加载自签名认证的https图片
- https 自签名配置
- 使用UIWebView加载网页
- UIWebView 加载网页
- UIWebView加载网页
- iOS网页-加载网页(UIWebView)
- UIWebView 显示正在加载网页
- UIWebView之加载本地网页
- IOS UIWebView加载网页实例
- UIWebView加载本地网页乱码
- UIWebView 加载网页的方法
- swift 3.1. uiwebview 加载网页
- SQL经典查询
- Android Studio 2中通过getDeclaredFields获取到$change成员问题
- 操作系统实践之第二章(实验环境升级)
- bootstrap的模态框锁定可点击区域
- spark 集群部署可用方法
- UIWebView加载自签名Https网页
- Ping测试网络连接状态
- bzoj 1047: [HAOI2007]理想的正方形 (单调队列)
- python核心编程&条件判断与循环
- 加速iOS开发的28个第三方库
- RedisProxy 系统设计与使用说明
- VelocityTracker简介
- Android NDK开发(二) 底层实现HEX的编码和解码
- fastdfs nginx