I use the following Swift extension to check if the error is a timeout or other network error using Swift 4
extension Error { var isConnectivityError: Bool { // let code = self._code || Can safely bridged to NSError, avoid using _ members let code = (self as NSError).code if (code == NSURLErrorTimedOut) { return true // time-out } if (self._domain != NSURLErrorDomain) { return false // Cannot be a NSURLConnection error } switch (code) { case NSURLErrorNotConnectedToInternet, NSURLErrorNetworkConnectionLost, NSURLErrorCannotConnectToHost: return true default: return false } } }
source share