In the iOS app I am currently building, I am trying to show a message to the user when the session has timed out. I read the documentation for NSURLSessionDelegate
but couldn't find out any method for letting me know if the session has timed out. How do I go about doing this? Any help is appreciated.
You can call method this way:
let request = NSURLRequest(URL: NSURL(string: "https://evgenii.com/")!)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
if error != nil {
if error?.code == NSURLErrorTimedOut {
println("Time Out")
//Call your method here.
}
} else {
println("NO ERROR")
}
}
task.resume()
I am using following Swift extension
to check whether error is time-out 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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With