Swift - 重试直到网络可达

Swift - Retry until network is reachable

我正在尝试使用 URLSession.shared.dataTask 向服务器发送 URL 请求。

当网络不可达时,日志会显示"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.",不重试将不会发送任何内容。

有什么方法可以用来重新发送 URL 请求,直到网络可达?

您可以在先前请求的响应正文中发送新请求,特别是在错误关闭中。更好的做法是使用 Reachability 并仅在网络连接可用时发送请求

https://github.com/ashleymills/Reachability.swift

你的答案是肯定的,你可以使用 ReachabilitySwift 来监控你的应用程序中的网络变化,并在它可以访问 wifi 或蜂窝数据时收到通知并发送 URL 请求