uniapp app打包是否必须要用https协议接口?如何解决非https请求问题

在uniapp打包成APP时,是否强制要求所有接口必须使用HTTPS协议?我的后端接口目前是HTTP的,在真机运行时会被拦截导致请求失败。除了让后端升级HTTPS外,有没有其他临时解决方案?比如配置白名单或修改manifest.json等?求具体的操作步骤。

2 回复

uniapp打包App时,非必须使用https协议。可通过以下方法解决非https请求问题:

  1. 安卓平台:在manifest.json中配置networkSecurityPolicy允许http请求
  2. iOS平台:在info.plist中添加ATS例外配置
  3. 开发阶段:使用hbuilder内置浏览器调试时可忽略证书验证

建议正式环境还是使用https确保安全性。


在 UniApp 中,App 打包并不强制要求所有接口必须使用 HTTPS 协议,但需要注意以下几点:

  1. iOS 限制:从 iOS 10 开始,Apple 强制要求新上架 App 使用 HTTPS 协议。如果接口为 HTTP,需在 manifest.json 中配置白名单:

    {
      "app-plus": {
        "distribute": {
          "ios": {
            "ATS": {
              "NSAllowsArbitraryLoads": true
            }
          }
        }
      }
    }
    

    但此配置可能导致 App Store 审核不通过,建议优先使用 HTTPS。

  2. Android 限制:Android 9 (API 28) 及以上默认禁止 HTTP 请求,需在 manifest.json 中配置:

    {
      "app-plus": {
        "distribute": {
          "android": {
            "networkSecurity": {
              "cleartextTraffic": true
            }
          }
        }
      }
    }
    
  3. 通用解决方案

    • 服务端升级 HTTPS:最佳方案,申请 SSL 证书并部署。
    • 使用代理或中间层:通过服务器转发 HTTP 请求(如 Nginx 反向代理)。
    • 本地调试:开发阶段可使用 uni.request 直接请求 HTTP,但需配置上述白名单。

示例代码(UniApp 请求):

uni.request({
  url: 'http://example.com/api', // 非 HTTPS 地址
  success: (res) => {
    console.log(res.data);
  },
  fail: (err) => {
    console.error('请求失败:', err);
  }
});

总结

  • 生产环境强烈推荐使用 HTTPS。
  • 临时解决 HTTP 问题需配置平台白名单,但可能影响审核。
  • 长期方案是升级服务端支持 HTTPS,确保兼容性和安全性。
回到顶部