今天把现有APP在IOS9上编译了一次发现,所有请求都是timeout
2015-06-12 10:32:42.087 mcapp[8300:226922] Failed:Error
Domain=NSURLErrorDomain Code=-1001 "The request timed out."
UserInfo=0x7f9b18cdc420 {NSUnderlyingError=0x7f9b18f49e80 "The request timed
out.", NSErrorFailingURLStringKey=https://***.maichong.me/***/share-data?,
NSErrorFailingURLKey=https://***.maichong.me/***/share-data?,
_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102,
NSLocalizedDescription=The request timed out.}
webView加载URL:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://image.html"]]];
输出日志:
shouldStartLoadWithRequest url: http://image.html
shouldStartLoadWithRequest url: https://image.html
shouldStartLoadWithRequest url: http://image.html
didFailLoadWithError error: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x14e989f0 {NSErrorFailingURLKey=http://image.html, NSErrorFailingURLStringKey=http://image.html}
shouldStartLoadWithRequest url: https://image.html
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
仔细看了一下发现协议被修改成了https协议,难道是IOS9搞得鬼?
于是去查了一下IOS9的文档
果然找到这么一段:App Transport Security
App Transport Security
App Transport Security (ATS) lets an app add a declaration to its Info.plist
file that specifies the domains with which it needs secure communication. ATS
prevents accidental disclosure, provides secure default behavior, and is easy
to adopt. You should adopt ATS as soon as possible, regardless of whether
you’re creating a new app or updating an existing one.
If you’re developing a new app, you should use HTTPS exclusively. If you have
an existing app, you should use HTTPS as much as you can right now, and create
a plan for migrating the rest of your app as soon as possible.
正如上面说的provides secure default behavior
,现在https在iOS9上是默认的。并且劝告开发者尽可能使用https协议,而且要把需要开启https协议的域名在info.plist里声明。
解决方法
在info.plist里设置ATS
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
然后重新编译运行。
后记
苹果这次iOS9强制启用https,可能是受之前应用安全问题的考虑,还是希望大家都改成https协议,所以赶紧和服务器端商量,准备全面迁移到https协议上。