天天看点

iOS ATS(App Transport Security has blocked a cleartext HTTP (http://) resource load since it is i...

异常日志:

2018-01-04 15:33:42.270 01NSURLConnection[532:15138] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
2018-01-04 15:33:42.276 01NSURLConnection[532:14992] 连接错误 Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSErrorFailingURLStringKey=http://www.baidu.com, NSErrorFailingURLKey=http://www.baidu.com, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSUnderlyingError=0x78e21290 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSErrorFailingURLKey=http://www.baidu.com/, NSErrorFailingURLStringKey=http://www.baidu.com/, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}}}
           

解决方法:

  1. 在项目的根目录下找到Info.plist, 右键->Open As->Source code
    iOS ATS(App Transport Security has blocked a cleartext HTTP (http://) resource load since it is i...
    图1.png
  2. 在dict节点下加入如下代码:
<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
           
  1. 保存后即可正常访问Http请求

注意: iOS在访问Http请求时会出现这种问题,所以在创建项目时先加入此段代码。由于这段代码经常使用,故可加入代码段中,加入代码段时,去掉第一个尖括号,这样可以省去每次删除一个尖括号的操作。

继续阅读