Everyone who reads it! My iOS program needs to be connected to Internet and if connection is lost, report to me immidiatly. It could be anything: Airplane Mode, changed modes from LTE to 3G or Edge, turning WiFi on or off, going from WiFi to cellular signal or opposite. I already have a code, which allows to check is Internet available or not. There it is (from How to use SCNetworkReachability in Swift):
import SystemConfiguration
func connectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}) else {
return false
}
var flags : SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.Reachable)
let needsConnection = flags.contains(.ConnectionRequired)
return (isReachable && !needsConnection)
}
But this code works only if I call connectedToNetwork() function. For example, when I am sending POST request to server or pressing button. I could use a timer to execute this code every 10 seconds, but I think there has to be a better way. So I want to catch system events if something with connecting happens and then execute this code or get information about Internet was lost or available now. Thank you for future help!