Xcode7 创建HTTP请求报错

本文介绍了解决iOS9应用传输安全(ATS)策略阻止HTTP请求的问题方法。通过修改Info.plist文件允许特定域名的HTTP请求,或者全面转向HTTPS以符合ATS的安全要求。

最近在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)


    ### Xcode 15 中配置 Command Line Tools 的教程 在 macOS 开发环境中,Xcode 提供了一组强大的开发工具集,其中包括 Command Line Tools。以下是关于如何在 Xcode 15 中配置这些工具的具体方法: #### 安装 Command Line Tools 要安装 Xcode Command Line Tools,可以通过终端执行以下命令完成初始安装: ```bash xcode-select --install ``` 此命令会触发一个图形化界面提示用户确认安装请求[^1]。 #### 验证安装状态 一旦安装完成后,可通过验证路径是否存在来判断安装是否成功: ```bash xcode-select -p ``` 如果返回 `/Library/Developer/CommandLineTools` 则说明已正确安装[^2]。 #### 使用 Xcode 进行进一步验证 另一种方式是通过启动 Xcode创建一个新的项目来进行测试。当打开 Xcode 后,在新建项目的选项中选择 macOS 应用程序类别下的 **Command Line Tool** 图标时,如果没有报错,则表明环境已经正常加载了所需的工具链[^3]。 #### 版本检查 为了确保当前使用的 `xcode-select` 是最新版本或者支持的功能符合预期需求,可以运行如下指令查看其版本号信息: ```bash xcode-select --version ``` 这一步骤有助于开发者了解所依赖的基础组件情况[^4]。 #### 解决潜在问题 假如遇到某些异常状况比如链接错误或者其他与 CLT 相关的问题时,可能需要重置 xcode-select 设置回到出厂默认值再重新指定新的 SDK 路径等操作。具体做法如下所示: ```bash sudo xcode-select --reset ``` 上述动作需管理员权限才能生效,并且它能够帮助清除掉之前残留的数据从而让系统再次扫描最新的可用资源位置[^5]。 ### 总结 综上所述,对于想要学习怎样于 Xcode 15 上面适配好 Command Line Tools 的朋友们来说,按照以上几个方面去实践应该就可以顺利完成整个过程啦!
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值