You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic应用iOS14模拟器运行触发allowsSpecificHTTPSCertificateForHost nil host错误

报错说明

"nil host used in the call to allowsSpecificHTTPSCertificateForHost"
该报错出现在iOS14及以上版本运行Ionic应用的场景,本质是网络请求环节传入的host参数为空触发了系统ATS校验拦截,仅修改AppDelegate.m无法覆盖根因,以下是对应的排查和修复方案:

排查步骤
  • 确认项目所用网络插件版本:执行ionic cordova plugin list(Cordova架构)或npm ls @capacitor/core(Capacitor架构),重点核对cordova-plugin-advanced-http版本,低于3.0.0的版本存在iOS14兼容bug。
  • 检索全局网络请求代码,排查是否存在URL拼接时域名未初始化、为空或为undefined的情况。
  • 确认ATS配置是否正确写入Info.plist,而非仅在AppDelegate.m中添加逻辑,AppDelegate.m的代码不会覆盖Info.plist的ATS规则。
修复方案
  • 插件版本兼容修复:卸载低版本网络插件,运行cordova plugin remove cordova-plugin-advanced-http,再安装稳定兼容版cordova plugin add cordova-plugin-advanced-http@3.3.1,重新构建iOS包后测试。
  • 请求合法性校验:给所有网络请求添加前置校验,避免空host请求发起,参考代码:
// 发起请求前校验
const reqUrl = '你的请求地址'
try {
  const host = new URL(reqUrl).hostname
  if (!host) throw new Error('空host')
} catch (e) {
  console.error('非法请求地址:', reqUrl)
  return
}
  • ATS配置修正:无需修改AppDelegate.m,直接在项目config.xml中添加对应配置即可自动同步到Info.plist,参考配置:
<platform name="ios">
  <config-file parent="NSAppTransportSecurity" target="*-Info.plist">
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <false/>
      <key>NSExceptionDomains</key>
      <dict>
        <key>你需要豁免的域名</key>
        <dict>
          <key>NSExceptionAllowsInsecureHTTPLoads</key>
          <true/>
          <key>NSIncludesSubdomains</key>
          <true/>
        </dict>
      </dict>
    </dict>
  </config-file>
</platform>

配置完成后执行ionic cordova build ios重新构建项目即可生效。

  • Capacitor架构适配:升级@capacitor/ios到3.0及以上版本,直接在ios/App/App/Info.plist中添加上述ATS配置即可。

内容的提问来源于stack exchange,提问作者kshitij agrawal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 17:06:00