ios UIWebView加载https出现 load page error:Error Domain=NSURLErrorDomain Code=-1202 "此服务器的证书无效
来源:互联网 发布:安卓chroot运行linux 编辑:程序博客网 时间:2024/06/05 09:35
今天用UIWebView加载https时出现此服务器的证书无效,然后在网上找解决方法,发现并不是很完美,根据自己的理解修改如下:
1.在webview出现的XX.h文件中加入NSURLConnectionDelegate代理,并声明以下变量
NSURLConnection *_urlConnection;
NSURLRequest *_request;
BOOL _authenticated;
2.在xx.m文件中加入以下方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;{ if (!_authenticated) { _authenticated =NO; _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; // 网上好多教程这句写的request写的是_request _request = request; //网上好多教程这句代码忘记加了 [_urlConnection start]; return NO; } return YES;}#pragma mark - NURLConnection delegate- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ if ([challenge previousFailureCount] == 0) { _authenticated = YES; NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; } else { [[challenge sender] cancelAuthenticationChallenge:challenge]; }}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ // remake a webview call now that authentication has passed ok. _authenticated = YES; [self.webView loadRequest:_request]; // self.webView替换成自己的webview // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!) [_urlConnection cancel];}// 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];}
希望对大家有用,有意见可以在文章下面提
0 0
- ios UIWebView加载https出现 load page error:Error Domain=NSURLErrorDomain Code=-1202 "此服务器的证书无效
- UIWebView加载HTTPS站点出现NSURLErrorDomain code=-1202 SSL
- AFNetwork https请求 有时报错Error: Error Domain=NSURLErrorDomain Code=-1012的解决办法
- [iOS]Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://121.
- [iOS]Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
- IOS Error Domain=NSURLErrorDomain Code=-999 "(null)"
- ios Error Domain=NSURLErrorDomain Code=-999 "已取消"
- 错误:HTTP load failed (kCFStreamErrorDomainSSL, -9813)/Error Domain=NSURLErrorDomain Code=-1202
- Error Domain=NSURLErrorDomain Code=-1022
- Error Domain=NSURLErrorDomain Code=-999
- Error Domain=NSURLErrorDomain Code=-1022
- 关于使用UIWebView加载HTTPS站点出现NSURLErrorDomain code=-1202
- error ==Error Domain=NSURLErrorDomain Code=-1009
- error: Error Domain=NSURLErrorDomain Code=-1002
- iOS 真机调试报错error ==Error Domain=NSURLErrorDomain Code=-1009
- iOS Error = NSURLErrorDomain Code=-1022
- 调试突然报 Error Domain=NSURLErrorDomain Code=-1003
- Error Domain=NSURLErrorDomain Code=-999错误
- 斗地主游戏AI出牌
- unset不能return 因为返回的是void
- 使用autoconf对项目自动生成makefile
- 关于iframe子页面修改其父页面CSS样式
- 7.注解式控制器简介
- ios UIWebView加载https出现 load page error:Error Domain=NSURLErrorDomain Code=-1202 "此服务器的证书无效
- ASPxComoBox的联想输入功能
- UVa 116 - Unidirectional TSP(DP)
- java中for循环中为了去掉最后一个字符的优化
- poj——1847——Tram
- 五、奔腾的芯(INTEL)
- JVM垃圾收集算法简介
- firefox禁用缓存
- Linux 下挂载硬盘的 方法