Reachability is deprecated.
You can use NWPathMonitors to monitor the WiFi and Cellular connections.
A closure is called every time the connection status changes.
(The monitoring takes place on a background thread, so you need to dispatch the UI updates on the main thread.)
Setup:
import NetworkExtension
private let monitorWiFi = NWPathMonitor(requiredInterfaceType: .wifi)
Monitor:
monitorWiFi.pathUpdateHandler = { path in
/// This closure is called every time the connection status changes
DispatchQueue.main.async {
switch path.status {
case .satisfied:
print("PathMonitor WiFi satisfied, interface: \(path.availableInterfaces) gateways: \(path.gateways)")
default:
print("PathMonitor WiFi not satisfied: \(path.unsatisfiedReason)")
}
}
}
monitorWiFi.start(queue: DispatchQueue(label: "monitorWiFi"))