NSURLErrorDomain error code 1002 description

I am new to iOS development. I am trying to load JSON , here is my function:

func loadmyJSON (urlPath: String) { let url: NSURL = NSURL(string: urlPath)! let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in println("Task completed") if(error != nil) { // If there is an error in the web request, print it to the console println("error not nil::::::") println(error.localizedDescription) } var err: NSError? var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary if(err != nil) { // If there is an error parsing JSON, print it to the console println("JSON Error \(err!.localizedDescription)") } let results: NSArray = jsonResult["variants"] as NSArray dispatch_async(dispatch_get_main_queue(), { self.jsonTable = results self.productView!.reloadData() }) }) 

But I get this error:

 error not nil:::::: The operation couldn't be completed. (NSURLErrorDomain error -1002.) 

I can get JSON in the browser through the same URL. I tried reading THIS : but I cannot get a description.

What does this error code mean?

+7
source share
3 answers

NSURLErrorDomain error -1002 means NSURLErrorUnsupportedURL or kCFURLErrorUnsupportedURL . This indicates that an invalid URL was provided, in most cases there was no http:// in the URL prefix.

A list of all NSURLErrorDomain codes can be found here: https://developer.apple.com/documentation/foundation/1508628-url_loading_system_error_codes

+24
source

In addition to the answer above, one of the reasons why this could happen is

You may have set the Allow arbitrary downloads option to false in your Info.plist, and the URL you are trying to download is not on a secure server.

The solution to this problem is to either set it to true or switch to a secure connection, i.e. https:// .

Hope this helps someone. All the best.

+2
source

Some nonsense, but try as shown below:

  1. Firstly, you can try to check the problem on the simulator, i.e. run the application on the simulator and check if there is a problem.

  2. Class on the network or where is your API call function check if this is implemented

    request.allHTTPHeaderFields = ["Content-Type" : "application/json"]

  3. Right-click on the info.plist file. select open as source code.

check and find below if there are lines

 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 

if not, add the lines above before / dict:

  1. Also, make sure you use the URL from the local server and switch to another valid URL.

  2. Check your device’s internet connection.

0
source

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


All Articles