Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession error handling

Using Swift and NSURLSession. The NSError localizedDescription I get is very generic when I don't have internet connection (manually turned off wifi/cell network). It says "The operation couldn’t be completed."

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

I'm looking for a more specific message to send to the user. I enabled a breakpoint and inspected all objects but can't find anything good.

Prior to swift I was using AFNetworking with objective C:

failure:^(AFHTTPRequestOperation *operation, NSError *error) {

The error messages I would get here were very descriptive, something like "the internet connection appears to be offline"

like image 625
MobileMon Avatar asked Mar 18 '15 12:03

MobileMon


1 Answers

Swift 4

if let error = error as NSError?, error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet {
      //not connected
}
like image 60
Sazzad Hissain Khan Avatar answered Sep 21 '22 05:09

Sazzad Hissain Khan