NSURLSession Error Handling

Using Swift and NSURLSession. The localized NSError definition that I get is very general when I do not have an Internet connection (the wifi / cell network is manually disabled). It says: "The operation cannot be completed."

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

I am looking for a more specific message to send to the user. I turned on the breakpoint and checked all the objects, but did not find anything good.

Until quick, I used AFNetworking with a C lens:

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

The error messages I received here were very descriptive, something like "offline internet connection"

+5
source share
3 answers

Look at the code and domain resulting NSError object. You can diagnose why he could not evaluate them.

For example, the domain NSURLErrorDomain and the code -1009 means that you are not connected to the Internet. So, in Swift 3:

 if let error = error as? NSError, error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet { print("not connected") } 

You can see a list of these codes in Global Variables - a reference to the Foundation constant . Just check out the NSURL Domain.

Also find NSURLErrorNotConnectedToInternet by pressing command + shift + o , and then you will be NSURLError.h to the corresponding header ( NSURLError.h ). Personally for this kind of thing, I tend to trust the headings a bit more than the documentation. If you perform this search when working with Objective-C, you will even see a cross reference to CFURLError codes (for which you can either click command or command + shift + o and look for kCFURLErrorNotConnectedToInternet ), and if you examine them, you will see numeric values โ€‹โ€‹associated with these constants.

+19
source
 var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in if error == nil { //all ok do what you want to do } else { let alertController = UIAlertController(title: "Error", message: "Put your error message here", preferredStyle: .Alert) let cancelAction = UIAlertAction(title: "OK", style: .Cancel) { (action) in } alertController.addAction(cancelAction) self.presentViewController(alertController, animated: true) } }) 

This implementation shows a warning to the user that something went wrong. You can handle the error as you wish (for example, by repeating the request without annoying the user with a warning). UIAlertController is iOS 8 only. If you want to support iOS 7, show UIAlertView .

0
source

Do you mean operation.responseString for a specific error response from the backend? I just found it yesterday, check this in the AFHTTPSessionManager request failure block

 NSError *underError = error.userInfo[@"NSUnderlyingError"]; NSData *responseData = underError.userInfo[@"com.alamofire.serialization.response.error.data"]; 

Here is the question I asked on AFN / Github . Hope this will be helpful.

-1
source

Source: https://habr.com/ru/post/1215594/


All Articles