Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection
http网络请求 真机调试 遇到问题
Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection
这个错误明确指出了问题:您正在尝试加载一个非安全的HTTP连接 (http://www.test.ichengke.com/doLogin),而苹果的App Transport Security (ATS)策略要求所有连接必须使用安全的HTTPS。以下是针对此问题的完整解决方案:
🔐 解决方案(针对 www.test.ichengke.com)
1. 在Info.plist中添加ATS例外
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>test.ichengke.com</key>
<dict>
<!-- 允许HTTP连接 -->
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!-- 包含所有子域名 -->
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
2. 仅针对特定子域名配置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>www.test.ichengke.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
📌 重要注意事项
1. App Store审核说明
如果您的应用需要提交到App Store,必须提供合理的HTTP使用理由:
- 在App Store Connect的"App Store信息"部分添加说明:
我们使用HTTP连接访问www.test.ichengke.com,因为该服务尚未提供HTTPS支持。 我们已计划在2025年Q1前迁移到HTTPS。
2. 安全风险缓解
虽然允许HTTP连接,但可以添加额外安全措施:
// 在发送请求前验证域名
func isTrustedDomain(url: URL) -> Bool {
let trustedDomains = ["www.test.ichengke.com", "api.test.ichengke.com"]
return trustedDomains.contains(url.host ?? "")
}
// 使用URLSession时
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200,
let data = data {
// 处理数据
} else {
// 显示安全警告
}
}
3. 长期解决方案
强烈建议将服务迁移到HTTPS:
- 联系服务提供商申请SSL证书
- 使用Let's Encrypt获取免费证书
- 在服务器配置301重定向:
server { listen 80; server_name www.test.ichengke.com; return 301 https://$host$request_uri; }
🔧 高级配置选项
1. 仅限开发环境使用HTTP
<key>NSAppTransportSecurity</key>
<dict>
<!-- 开发环境配置 -->
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
然后在发布版本中移除这些配置。
2. 动态ATS配置
// 在App启动时配置ATS
func configureATS() {
#if DEBUG
// 开发环境允许HTTP
if let path = Bundle.main.path(forResource: "Info", ofType: "plist"),
var dict = NSDictionary(contentsOfFile: path) as? [String: Any] {
var ats: [String: Any] = [
"NSAllowsArbitraryLoads": true,
"NSExceptionDomains": [
"test.ichengke.com": [
"NSExceptionAllowsInsecureHTTPLoads": true,
"NSIncludesSubdomains": true
]
]
]
dict["NSAppTransportSecurity"] = ats
(dict as NSDictionary).write(toFile: path, atomically: true)
}
#endif
}
🚀 验证解决方案
- 清理Xcode缓存:
rm -rf ~/Library/Developer/Xcode/DerivedData/* - 重置模拟器网络设置:
xcrun simctl erase all - 在真机上测试:
- 删除应用后重新安装
- 确保设备时间设置正确
- 使用网络调试工具:
// 在AppDelegate中添加 URLSessionConfiguration.default.waitsForConnectivity = true URLSessionConfiguration.default.timeoutIntervalForRequest = 30
⚠️ 如果问题仍然存在
- 检查URL是否正确:
// 确保URL没有多余空格或特殊字符 guard let url = URL(string: "http://www.test.ichengke.com/doLogin") else { print("无效URL") return } - 验证服务器响应: 在Safari中访问
http://www.test.ichengke.com/doLogin,确认服务正常运行。 - 使用curl测试:
curl -I http://www.test.ichengke.com/doLogin # 检查返回的HTTP状态码 - 考虑使用HTTPS中间层:
然后在应用中使用# 在您的服务器上配置反向代理 server { listen 443 ssl; server_name proxy.yourdomain.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; location / { proxy_pass http://www.test.ichengke.com; proxy_set_header Host $host; } }https://proxy.yourdomain.com/doLogin
通过以上配置,您应该能够解决这个ATS策略导致的连接问题。长期来看,迁移到HTTPS是最安全可靠的解决方案。
在北京的灯中,有一盏是我家的。这个梦何时可以实现?哪怕微微亮。北京就像魔鬼训练营,有能力的留,没能力的走……

浙公网安备 33010602011771号