#ATS设置 按照惯例写一个UIWebView,用来加载网页:git
_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一下看看加载出来了吗?若是发现屏幕一片白并无出现网页内容,不要惊讶,看看你的控制台是否是报出如下错误:github
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
复制代码
这个怎么解决呢?没错ATS设置:去plist文件里添加一项App Transport Security Settings,它是个字典类型,给它增长一对键值,键:Allow Arbitrary Loads ,值设为YES。web
以上,搞定ATS设置,网页成功加载了:api
若是你的网页是self signed website,那么你的屏幕应该仍是一片白,而且控制台又报错:安全
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
复制代码
注意:这两次错误并非同样的,后面数字代码不一样 (并不太清楚这个码表明的意思,有知道的朋友请留言告知,感谢。)bash
##NSURLConnectionui
使用webview加载自签名https站点的时候,必须在请求的时候将该站点设置为安全的,才能继续访问。因此咱们须要在webview开始加载网页的时候首先判断判断该站点是否是https站点,若是是的话,先让他暂停加载,用NSURLConnection 来访问改站点,而后再身份验证的时候,将该站点置为可信任站点。而后在用webview从新加载请求。this
直接上代码:url
#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]; } 复制代码
以上设置,可成功加载自签名网页。spa
可是,虽然加载成功,可是控制台仍是报了如下错误:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
复制代码
##最后 推荐参考连接: stackoverflow Q1 stackoverflow Q2