[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接口了。

相关推荐
小阿宁的猫猫1 小时前
CSRF漏洞的原理、防御和比赛中的运用
安全·http·xss·csrf
Yorelee.3 小时前
ms-swift在训练时遇到的部分问题及解决方案
开发语言·nlp·transformer·swift
教练、我想打篮球6 小时前
120 同样的 url, header, 参数, 使用 OkHttp 能够成功获取数据, 使用 RestTemplate 报错
http·okhttp·resttemplate·accept
zfj3216 小时前
websocket为什么需要在tcp连接成功后先发送一个标准的http请求,然后在当前tcp连接上升级协议成websocket
websocket·tcp/ip·http
杀手不太冷!6 小时前
Jenkins的安装与使用;git clone url的时候,url为http和ssh时候的区别
git·http·jenkins
游戏开发爱好者87 小时前
苹果 App 上架流程,结合 Xcode、CI 等常见工具
macos·ios·ci/cd·小程序·uni-app·iphone·xcode
irisart8 小时前
第二章【NGINX 开源功能】—— HTTP 服务器(下)
nginx·http·开源
崽崽长肉肉1 天前
swift中的知识总结(一)
ios·swift
Neolnfra1 天前
渗透测试标准化流程
开发语言·安全·web安全·http·网络安全·https·系统安全
fiveym1 天前
Apache HTTP 服务搭建全攻略
网络协议·http·apache