最近在Xcode 7中向服务器发送请求访问JSON数据时, 控制台打印了以下错误信息:

Application 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.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

看原因应该是获取到的NSData数据为空, 所以在调用+ JSONObjectWithData: 方法时出现错误. 检验http链接可以正常访问. 后来发现在iOS9应用通讯安全策略进行了升级, 已不再支持http这种不安全的协议(What's New in iOS 9.0):

App Transport Security

App Transport Security (ATS) enforces best practices in the secure connections between an app and its back end. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt; it is also on by default in iOS 9 and OS X v10.11. 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. In addition, your communication through higher-level APIs needs to be encrypted using TLS version 1.2 with forward secrecy. If you try to make a connection that doesn't follow this requirement, an error is thrown. If your app needs to make a request to an insecure domain, you have to specify this domain in your app's Info.plist file.

至于为什么http不安全, http是超文本传输协议, 信息采用明文传输, 而https则使用SSL加密传输协议进行传输. 既然服务器的链接并不是我们前端所能决定的, 如果一定要发送http协议的请求, 可以修改当前项目的Info.plist文件来实现:

方式一: 使用文本编辑Info.plist, 在当中添加:

<!--回到过去不安全的HTTP网络请求,能任意进行HTTP请求 (不建议这样做, 原因见下文)-->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

方式二: 在Info.plist中添加:

以上两种方式所实现的效果是一致的, 但是并不严谨, 建议有选择的允许HTTP请求(这个操作方法与上文"方式一"相同):

<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>域名.com</key>
<dict>
<!--允许子域名:subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--允许App进行不安全的HTTP请求-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--在这里声明所支持的 TLS 最低版本-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>

当然, 以上方法都是建立在所访问的请求是HTTP类型的基础上, 一劳永逸的方法就是让服务端升级使用TLS 1.2 SSL加密请求的HTTPS协议.

服务器已支持TLS 1.2 SSL ,但iOS9上还是不行,还要进行链接里的适配操作。”那是因为:ATS只信任知名CA颁发的证书,小公司所使用的 self signed certificate,还是会被ATS拦截。对此,建议使用链接中给出的NSExceptionDomains,并将你们公司的域名挂在下面。

参考: iOS9AdaptationTips

--Xcode Version 7.0 beta (7A120f)

Xcode7 创建HTTP请求报错的更多相关文章

  1. Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

    Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...

  2. Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport

    Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...

  3. Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requir

          今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport S ...

  4. Xcode7.1 网络请求报错

    The resource could not be loaded because the App Transport Security policy reguir 原因:iOS9引入了新特性App T ...

  5. Android版本28使用http请求报错not permitted by network security policy

    Android版本28使用http请求报错not permitted by network security policy android模拟器调试登录的时候报错 CLEARTEXT communic ...

  6. eclipse 向HDFS中创建文件夹报错 permission denied

    环境:win7  eclipse    hadoop 1.1.2 当执行创建文件的的时候, 即: String Path = "hdfs://host2:9000"; FileSy ...

  7. nuget包管理nuget服务器发布包时出现请求报错 406 (Not Acceptable)

    在window服务器上部署nuget服务器时,发布包时出现请求报错 406 (Not Acceptable) 验证用户名.密码正确的情况下,还是出现上面错误.后面跟踪服务器日志,发现window\te ...

  8. Redis创建集群报错

    Redis创建集群报错: 1:任何一个集群节点中都不能存在数据,如果有备份一下删除掉aof文件或rdb文件 2: nodes-集群端口.conf 文件存的会有报错记录,所以该文件也要删除

  9. git https 请求报错 504

    git https 请求报错 504 原因可能是因为设置了代理,ubuntu/deepin 系统可以检查 /etc/profile ~/.bashrc 内有没有设置 https 的代理. 有的话,去掉 ...

随机推荐

  1. angularJs之$watch监听属性变化访问后台

  2. 十分钟学会python

    1.raw_input的使用 从键盘读取信息,返回字符串. 例: hrs = raw_input("Enter Hours:")pay=raw_input("Enter ...

  3. 处理 httprequest post 编码问题

    site :http://www.cnblogs.com/artwl/archive/2012/03/07/2382848.html 在JavaScript中推荐的做法是用encodeURI对URI的 ...

  4. PHP之十六个魔术方法详解

    https://segmentfault.com/a/1190000007250604#articleHeader14

  5. opencv的学习笔记4

    通常更加高级的形态学变换,如开闭运算.形态学梯度.“顶帽”.“黑帽”等等,都是可以由常用的腐蚀膨胀技术结合来达到想要的效果. 1.开运算:先腐蚀后膨胀,用于用来消除小物体.在纤细点处分离物体.平滑较大 ...

  6. Excel应该这么玩——3、命名表格:彻底改造公式

    上一篇介绍了通过命名列将公式中的地址引用去掉,让公式更容易理解.但是之前的命名列只是针对单个表格的,如何引用其他表格的列是本篇的主要内容. 1.创建命名表格 命名表格的前提的创建表格,这个操作上一篇已 ...

  7. python脚本利用windows计划定时执行

  8. transient关键字

    transient关键字的英文意思是:瞬态,由此可见是瞬间的,不可固定的. 会不会与对象的状态等等有关系呢? 网上找了一下资料是跟对象的序列化有关. transient的作用 一个对象只要实现了Ser ...

  9. 用CSS3实现文字描边

    CSS3作为新兴的前端技术可以实现很多复杂变化的效果,比如文字描边. 这里主要用到text-shadow属性,顾名思义就是为文字加上阴影效果.例: text-shadow:10px 5px 2px # ...

  10. js页面刷新之实现普通页面

    准备面试题目的时候遇到了页面刷新,就整理了一下,网上查找,大概就是八种方法,但是自己测试的时候出现了几个问题,跟大家分享: 首先准备一个测试页面: <!--html代码--> <h1 ...