Xcode7 Https 在plist上添加NSAppTransportSecurity -NSAllowsAtbritraryLoads
来源:互联网 发布:抓小三软件下载 编辑:程序博客网 时间:2024/06/05 17:16
Xcode7 创建HTTP请求报错
最近在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
协议.
- Xcode7 Https 在plist上添加NSAppTransportSecurity -NSAllowsAtbritraryLoads
- xcode7.1 网路问题http/https NSAppTransportSecurity
- 在xcode7.1 上创建动画
- 关于在Xcode7.0.1上使用CocoaPods
- 在Xcode7上实现真机调试
- React-Native 在iOS9之后,网络请求默认为Https请求,如需支持Http,修改info.plist文件添加键值对设置允许http访问。
- Xcode7 https -- http
- iOS9 Xcode7 https解决方法
- 在docker上部署https
- 如何在Xcode7.2中添加空模板
- 在xcode7中进行http网络请求 需要添加字段
- iOS如何在Xcode7中手动添加Precompile Prefix Header?
- iOS9 - NSAppTransportSecurity
- Cocos2D在Xcode7和iOS 9.2上IMP调用出错
- iOS10在Xcode7.3上如何真机测试
- xcode7.0https安全协议
- xcode7以后,访问https服务器
- 把plist里数据显示在textField上
- Unity3d 人物控制器CharacterController
- 未能加载文件或程序集“”或它的某一个依赖。找到的程序集清单定义与程序集引用不匹配。
- 将Excel中的数据保存到Oracle中
- 浅谈数据中心(IDC)网络大二层技术
- Linux more命令的简单实现
- Xcode7 Https 在plist上添加NSAppTransportSecurity -NSAllowsAtbritraryLoads
- 读《符号逻辑讲义》
- Web项目中的Servlet映射
- eclipse(优化10) eclipse中vm文件样式修改
- jsp页面使用C标签对时间日期进行判断
- Java多线程机制
- 排序算法
- 文件重定向
- Gson使用