[Swift]Xcode模拟器无法请求http接口问题

1.以前偷懒一直是这样设置

bash 复制代码
<key>NSAppTransportSecurity</key>
<dict>
	<key>NSAllowsArbitraryLoads</key>
	<true/>
	<key>NSAllowsArbitraryLoadsInWebContent</key>
	<true/>
</dict>

现在我在Xcode16.3上,这种设置方式在真机上能请求http(应该是设备开启了开发者模式),但在模拟器上请求http是被拒绝的。报错如下:

Cannot start load of Task <AAC1C590-5604-43A7-984F-3D486C5996DF>.<1> since it does not conform to ATS policy

Task <AAC1C590-5604-43A7-984F-3D486C5996DF>.<1> finished with error [-1022] Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSErrorFailingURLStringKey=http://114.55.151.190:9527/api/onlineGoods/getGoodsList?page=1\&pageSize=20, NSErrorFailingURLKey=http://114.55.151.190:9527/api/onlineGoods/getGoodsList?page=1\&pageSize=20, _NSURLErrorRelatedURLSessionTaskErrorKey=(

"LocalDataTask <AAC1C590-5604-43A7-984F-3D486C5996DF>.<1>"

), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <AAC1C590-5604-43A7-984F-3D486C5996DF>.<1>, NSUnderlyingError=0x60000129bde0 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}}

问题就是由于 App Transport Security (ATS) 默认限制了非 HTTPS 请求。

2.现在需要这样设置

bash 复制代码
<key>NSAppTransportSecurity</key>
<dict>
      <key>NSAllowsArbitraryLoads</key> 
      <false/>
       <key>NSExceptionDomains</key>
       <dict>
            <key>example.com</key> <!--Include your domain at this line -->
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
       </dict>
</dict>

这里NSAllowsArbitraryLoads必须是false,因为它不允许所有不安全的连接,但例外列表允许连接到一些没有 HTTPS 的域。

这样模拟器上就能正常请求http接口了。

相关推荐
pengyi87101517 小时前
HTTP代理抓包核心原理,全面读懂请求与响应数据逻辑
网络·网络协议·http
人月神话-Lee19 小时前
【图像处理】高斯模糊——最优雅的模糊算法
图像处理·人工智能·算法·ios·ai编程·swift
yqcoder19 小时前
HTTP 进化论:从“单车道土路”到“磁悬浮列车”
网络·网络协议·http
小短腿的代码世界19 小时前
QHttpEngine深度解析:Qt嵌入式HTTP服务端的工业级架构与性能调优
qt·http·架构
pengyi87101520 小时前
HTTP代理抓包实操教程,零基础监控IP请求与响应数据
网络协议·tcp/ip·http
Daniel_Coder20 小时前
iOS Widget 开发-20:从旧版 API 迁移到 iOS 17+ 现代 Widget
ios·swift·widget·widgetcenter
Daniel_Coder20 小时前
iOS Widget 开发-19:Widget 调试与单元测试
ios·单元测试·swift·widget·widgetcenter
yqcoder2 天前
数据的“包装方式”:深入解析 HTTP Content-Type
网络·网络协议·http
wu@555552 天前
使用acme生成免费https泛域名证书(通配符证书)
网络协议·http·https
00后程序员张2 天前
Windows 下怎么生成 AppStoreInfo.plist?不依赖 Xcode 的方法
ide·macos·ios·小程序·uni-app·iphone·xcode