Note : not require to check internet connection in all view controller just add code on launch app.
import Network
let monitor = NWPathMonitor()
let queue = DispatchQueue(label: "NetStatus_Monitor")
monitor.start(queue: queue)
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.InternetConnectionAlert("Warnning", "We are connected to internet")
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.InternetConnectionAlert("Warnning", "We are not connected to internet")
}
}
print(path.isExpensive)
}
private func InternetConnectionAlert(_ title : String, _ Body : String) {
let alert = UIAlertController(title: title, message: Body, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("cancel")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}}))
UIApplication.shared.keyWindow?.rootViewController?.present(alert,animated: true,completion: nil)
}
if you want to stop network monitoring
monitor.cancel()