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

本文介绍在Xcode7.0beta版中遇到的网络访问失败问题及解决方法。由于App Transport Security(ATS)政策变更,需将项目的网络连接方式从HTTP改为HTTPS。文中提供了在Info.plist文件中配置NSAppTransportSecurity来解决此问题的具体步骤。

今天升级Xcode 7.0 bata发现网络访问失败。
输出错误信息

<code style="padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;">The resource could not be loaded because the App Transport Security policy requires the <span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">use</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">of</span> a secure <span class="hljs-keyword" style="color: rgb(133, 153, 0);">connection</span>.
</span></span></code>

Google后查证,iOS9引入了新特性App Transport Security (ATS)。详情:App Transport Security (ATS)

新特性要求App内访问的网络必须使用HTTPS协议。
但是现在公司的项目使用的是HTTP协议,使用私有加密方式保证数据安全。现在也不能马上改成HTTPS协议传输。

最终找到以下解决办法:

  1. 在Info.plist中添加NSAppTransportSecurity类型Dictionary
  2. NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES
在iOS开发中,App Transport Security(ATS)是苹果公司引入的一项安全机制,旨在确保应用的网络通信使用HTTPS协议并符合最新的加密标准。当服务器不支持HTTPS或使用的TLS版本低于要求时,会触发错误 `Code=-1022`,提示信息为“The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.”[^2] ### 配置 ATS 异常以解决网络请求失败 如果服务器尚未支持HTTPS或仅支持较低版本的TLS(如TLS 1.1),可以在 `Info.plist` 文件中配置ATS异常规则,从而允许应用与该服务器进行非加密或低版本TLS通信。 #### 1. **完全禁用 ATS** 适用于短期内无法升级服务器的情况,但不推荐长期使用: ```xml <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> ``` 此配置将绕过所有ATS限制,允许应用访问任何HTTP资源[^4]。 #### 2. **为特定域名配置 ATS 异常** 若只需对特定域名放宽限制,可采用更细粒度的配置方式: ```xml <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>yourdomain.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSTemporaryExceptionMinimumTLSVersion</key> <string>TLSv1.1</string> </dict> </dict> </dict> ``` 上述配置表示对 `yourdomain.com` 域名及其子域名允许使用HTTP协议,并接受最低TLS版本为 TLSv1.1 的连接。 --- ### 检查与调试 ATS 相关问题 当ATS阻止某个请求时,Xcode控制台通常会输出类似以下的日志信息: ``` 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. ``` 通过分析此类日志,可以快速识别哪些请求被ATS拦截,并据此调整 `Info.plist` 配置[^2]。 --- ### 注意事项 - **App Store 审核政策**:虽然可以在开发阶段使用 `NSAllowsArbitraryLoads` 来绕过ATS,但在提交到App Store时,Apple鼓励开发者尽可能遵循ATS要求。对于新应用或重大更新,若无合理理由,Apple可能会拒绝使用 `NSAllowsArbitraryLoads` 的应用。 - **安全性考虑**:放宽ATS限制虽然能解决当前的网络请求失败问题,但也可能带来潜在的安全风险,如中间人攻击(MITM)。因此建议尽快推动服务器端的HTTPS迁移工作。 --- ### 示例代码:ATS异常配置 以下是完整的 `Info.plist` 配置示例,针对特定域名放宽ATS限制: ```xml <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>example.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSTemporaryExceptionMinimumTLSVersion</key> <string>TLSv1.1</string> </dict> </dict> </dict> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值