1

consider this piece of code:

func load(url: URL?, callback: ((UIImage?) -> Void)? = nil) -> UIImage? {
    guard let url = url else {
      return nil
    }

    if let image = cache[url] {
        return image
    }
    guard let callback = callback else {
        return nil
    }
    var request = URLRequest(url: url)
    request.httpMethod = "GET"
    let config = URLSessionConfiguration.default
    config.timeoutIntervalForRequest = 10
    let session = URLSession(configuration: config, delegate: nil,         delegateQueue: OperationQueue.main)
    let task = session.dataTask(with: request) { data, response, error in
        guard let data = data else {
            return
        }
        DispatchQueue.global().async {
            let image = UIImage(data: data)
            DispatchQueue.main.async {
                self.cache[url] = image
                callback(image)
            }
        }
    }
    task.resume()
    return nil
}

We have run into an issue where in session.dataTask the data is returned as nil and the error is some sort of undecipherable NSURLError. Debugging into it does not give any useful leads to me as to what the problem might be. We have been able to determine that some URLs work and some don't but it is not clear where the problem lies.

The url in question is this:

http://website.com////assets//P//wa//_resampled//paddedimage400400-apicture.gif

This is what I get from copying the error, this reinforces again that something is wrong with the url:

error   Error?  (instance_type = 0x0000608000246930 -> 0x0000000106a2ff48 (void *)0x0000000106a2ff20: NSURLError)   some

A url that works is this one:

https://api.website.com/assets/P/wa/_resampled/picture.jpg

I mean obviously there are differences between both URLs but I can assure you that both work in the browser and correctly display an image.

Does anybody know how to decipher the NSURLError or get it into some readable format?

If not, does anybody have a specification for the formats that NSURL can work with and that we might be violating?

Nico
  • 11
  • 7
  • Any `NSError` instance thrown by the Apple frameworks has a `domain`, an error `code` and a localized description (`localizedDescription`) – vadian Jun 28 '17 at 09:35
  • No need to write code to download image. Here is the fantastic library https://github.com/rs/SDWebImage. Use it! – Parimal Jun 28 '17 at 09:39
  • @vadian Thanks that is actually useful and might get us somewhere. `The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.` – Nico Jun 28 '17 at 10:09
  • Look at https://stackoverflow.com/questions/32631184/the-resource-could-not-be-loaded-because-the-app-transport-security-policy-requi – vadian Jun 28 '17 at 10:11
  • @vadian Jup, this is what fixed it. Thanks man. – Nico Jun 28 '17 at 15:25

2 Answers2

0

You can check if the error is not nil and then cast it to NSError.

if let error = error {
    let nsError = error as NSError
    print(nsError.domain)
    print(nsError.code)
    print(nsError.localizedDescription)
    // ... check additional properties
}
florieger
  • 1,310
  • 12
  • 22
0

I was able to use localizedDescription to read what the problem was. Then this The resource could not be loaded because the App Transport Security policy requires the use of a secure connection fixed it.

Nico
  • 11
  • 7