Hello,
I'm looking for a way to detect using NWPathMonitor
when the iOS device is connected to a router but not to the internet.
As an example a mobile router WiFi without SIM.
In settings I'm able to switch the connection to its WiFi, once connected a label below the SSID shows Not connected to the internet
.
I would like to show the same thing to the user inside my app, but unfortunately I always get the satisfied
answer.
Am I missing something in configuring NWPathMonitor
or reading the answer?
final class InternetConnectionMonitor {
lazy var internetConnectionStatusPublisher: AnyPublisher<InternetConnectionStatus, Never> = {
_internetConnectionStatusSubject
.compactMap{ $0 }
.eraseToAnyPublisher()
}()
var lastInternetConnectionStatus: InternetConnectionStatus? {
_internetConnectionStatusSubject.value
}
private let _internetConnectionStatusSubject = CurrentValueSubject<InternetConnectionStatus?, Never>(nil)
private let pathMonitor = NWPathMonitor()
private let pathMonitorQueue = DispatchQueue(label: "com.xxxxx-network-monitor", qos: .default)
init() {
startPathMonitoring()
}
private func startPathMonitoring() {
pathMonitor.pathUpdateHandler = { [weak self] path in
guard let self else { return }
let networkStatus = InternetConnectionStatus(from: path)
self._internetConnectionStatusSubject.send(networkStatus)
}
pathMonitor.start(queue: pathMonitorQueue)
}
}