The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
问题
在根据《IOS开发指南 从helloworld到AppStore上架 第四版》学习的时候,看到第八章『树形导航结构』使用WKWebView进行三级导航展示详情,发现白屏
解决方案
WKWebView根据『WKNavigationDelegate』协议有几个方法,可以追踪加载进度
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
NSLog(@"准备加载页面");
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
NSLog(@"开始加载页面");
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"已加载全部页面");
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"加载失败1");
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"加载失败2%@",error.userInfo);
}
重新执行后,发现进入了didFailProvisionalNavigation
方法,这个时候我们打印error.code
得到值为-1022
。
2017-09-22 11:08:40.096041+0800 treeNavigation[6176:2793155] http://baike.baidu.com/view/3961.htm
2017-09-22 11:08:40.171849+0800 treeNavigation[6176:2793155] 准备加载页面
2017-09-22 11:08:40.184887+0800 treeNavigation[6176:2793155] 加载失败2{
NSErrorFailingURLKey = "http://baike.baidu.com/view/3961.htm";
NSErrorFailingURLStringKey = "http://baike.baidu.com/view/3961.htm";
NSLocalizedDescription = "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.";
NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1022 \"(null)\"";
"_WKRecoveryAttempterErrorKey" = "<WKReloadFrameErrorRecoveryAttempter: 0x17403eae0>";
}
看到这里基本上就能猜到https
的问题了,然后我们将url进行一次搜索替换,就可以了。
self.url = [self.url stringByReplacingOccurrencesOfString:@"http://" withString:@"https://"];