本篇文章由:http://xinpure.com/nsurlsessionnsurlconnection-http-load-failed-kcfstreamerrordomainssl-9802/
錯誤描述
環境:
iOS9 / Xcode7, Cordova
錯誤提示:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
百度/Google之后 不難發現報錯的原因
這是因為 iOS9
增加了新特性 App Transport Security (ATS)
。詳情:App Transport Security (ATS)
新特性要求 App 內訪問的網絡必須使用HTTPS協議並且改成 TLS1.2
協議進行傳輸
解決方法
網上提供了一種方不遵循 iOS9
新特性的規則,將 AppTransportSecurity
轉為使用用戶自定義的設置的方法,據說可以解決此問題~
講的最多的一種方法就是修改 (...)/platforms/ios/YOURAPP/YOURAPP-Info.plist
文件,添加以下代碼:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
還有一種修改 info.list
文件的方法:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>YOUDOMAIN(e.g. baidu.com)</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
然而這兩種方法,我都測試過,並沒有什么用。。。
只能再找方法試試了,找到了這個:修改 (...)/platforms/ios/嘎嚓嘎嚓/Classes/AppDelegate.m
文件,添加以下代碼:
@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
@end
哈哈。。。果然有用,簡直 Nice!
不過,info.list
文件是之前修改過的,然后就覺得是不是只需要修改 AppDelegate.m
文件就可以呢?
經過驗證是不行的,必須同時修改 info.list
和 AppDelegate.m
文件才可以解決此問題。
info.list
添加第一種代碼即可:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
參考鏈接
http://forum.ionicframework.com/t/nsurlsession-nsurlconnection-http-load-failed/32898