What does NSURLConnection's error code "-1009" mean?

47,705

Solution 1

Since the error returned should be within the NSURLErrorDomain, the code -1009 means:

NSURLErrorNotConnectedToInternet

Returned when a network resource was requested, but an internet connection is not established and cannot be established automatically, either through a lack of connectivity, or by the user's choice not to make a network connection automatically.

Solution 2

With Swift, you can use the NSURLError enum for NSURL error domain check:

switch NSURLError(rawValue: error.code) {
case .Some(.NotConnectedToInternet):
    print("NotConnectedToInternet")
default: break
}

Swift 3:

switch URLError.Code(rawValue: error.code) {
case .some(.notConnectedToInternet):
    print("NotConnectedToInternet")
default: break
}

Swift 4:

switch URLError.Code(rawValue: error.code) {
case .notConnectedToInternet:
    print("NotConnectedToInternet")
default: break
}

Solution 3

It's NSURLErrorNotConnectedToInternet which means, well, that you're not connected to the internet... :)

You can find the error codes in NSURLError.h.

Share:
47,705
jxx
Author by

jxx

Updated on April 17, 2020

Comments

  • jxx
    jxx about 4 years

    When I send a request and get an error with the error code -1009, what does that mean? I'm not sure how to handle it.

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
          NSLog(@"connection didFailWithError");
    
       if(error.code==-1009){       
          //do something           
       }    
    }
    
  • Saeed Rahmatolahi
    Saeed Rahmatolahi over 6 years
    I used this But in the first line the app won't allow me to run I am using swift 4
  • Saeed Rahmatolahi
    Saeed Rahmatolahi over 6 years
    the swift show me this massage when I use this code Expected declaration should I use it in view did load or not?
  • ricardopereira
    ricardopereira over 6 years
    @SaeedRahmatolahi sorry, I didn't get it. What message?
  • Saeed Rahmatolahi
    Saeed Rahmatolahi over 6 years
    the app won't let me to run because of this Error : Expected declaration
  • ricardopereira
    ricardopereira over 6 years
    @SaeedRahmatolahi I think you have the code in the wrong place. Make sure the code is inside the body of any function. It can not be outside.
  • Saeed Rahmatolahi
    Saeed Rahmatolahi over 6 years
    Yes thank you but when this code inside the body wants to run I will get fatal Error because the error in the first line is nil how can I avoid that?
  • ricardopereira
    ricardopereira over 6 years
    @SaeedRahmatolahi Please open a question and provide some code to look into it. Thanks