UIWebView是实战开发中常用的UI组件之一,通过给定制定网址就可以将网页展示在应用内。今天从网上找了个Demo跟着做了下,参考这个博客(http://www.cnblogs.com/zhuqil/archive/2011/07/28/2119923.html ).代码本身没问题。但在我的环境(Xcode 7.0 + OSX 10.10)下运行,出问题了。问题的表现为,无论如何也连不上网站,返回错误信息【iOS webView error code --1200】。最后搜索发现问题的关键在HTTP协议上。下面是搜索到得问题解决方案:
As explained in the keynote, by default all apps built with iOS 9 SDK are opted-in to Application Transport Security. HTTPS (with the most secure options currently available - maybe your server is using weaker keys or something) is required. You can opt out with a plist key - search these forums for "NSAppTransportSecurity" for several other threads on the topic.
iOS9让所有的HTTP默认使用了HTTPS,原来的HTTP协议传输都改成TLS1.2协议进行传输。直接造成的情况就是App发请求的时候弹出网络无法连接。解决办法就是在项目的info.plist 文件里加上如下节点:
问题便迎刃而解。
期间也出现了一个Error 999,原因是:一个页面在未完成加载之前,又收到下一个请求,则会出现一个错误,这个错误码就是999
要解决这个问题也很简单,在回调函数
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//添加这个条件判断即可
if ([error code] != NSURLErrorCancelled) {
//Your code
}
}