0

I am trying to download an image from this url:

https://​i.ebayimg.com/00/s/MTA2NlgxNjAw/z/TaoAAOSwjkdZ57Jm/$_2.jpg

Using this code:

    guard let escapedQuery = rawUrl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed),
    let url = URL(string: escapedQuery) else { return }

    // check if image is new image, if not, stick with the old image
    if(previousUrl != rawUrl){
        previousUrl = rawUrl
        showActivityIndicatory()
        print("Download Started")
        // download the image from the provided url
        getDataFromUrl(url: url) { data, response, error in
            guard let data = data, error == nil else {
                DispatchQueue.main.async() {
                    // stop the animation/loading icon
                    self.imageLoadingIndicator.stopAnimating()
                    // set a default placeholder image
                    self.image = #imageLiteral(resourceName: "CellDefaultImage")
                }
                return
            }
            print(response?.suggestedFilename ?? url.lastPathComponent)
            print("Download Finished")
            DispatchQueue.main.async() {
                self.image = UIImage(data: data)
                self.imageLoadingIndicator.stopAnimating()
            }
        }
    }

}

func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
    // try to download the raw data on a different thread to provide optimal perfomance regarding UI
    URLSession.shared.dataTask(with: url) { data, response, error in
        completion(data, response, error)
    }.resume()
}

func showActivityIndicatory() {
    imageLoadingIndicator.center = self.center
    imageLoadingIndicator.startAnimating()
}

But I am constantly running into : HTTP load failed (error code: -1200 [3:-9802]) or error code -1002 etc.

Could someone point out what is wrong, perhaps my image url formatting is wrong?

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
  • Hey! I don't have too much time right now but maybe [this might help you](https://stackoverflow.com/questions/32755012/nsurlsession-nsurlconnection-http-load-failed-9802) – Akaino Jan 19 '18 at 09:39
  • Hey, thanks for the reply, with other images, such as https://www.google.be/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png it is working – Philippe Creytens Jan 19 '18 at 09:40
  • You might also want to check the ATS. There is a great article overe [here](https://blog.beecomedigital.com/2015/10/26/nsurlsessionnsurlconnection-http-load-failed-when-trying-to-stream-an-office-365-video-on-ios-9/). It's iOS 9 but I'm pretty sure it still applies. Can't tell why one works and the other won't though. Sorry! – Akaino Jan 19 '18 at 09:49
  • 1
    Not related, but the url does not contain a query so it's supposed to be `CharacterSet.urlPathAllowed`. – vadian Jan 19 '18 at 09:57
  • Hey the ATS has been set in the info.plist, and is not changing anything to the outcome, sadly – Philippe Creytens Jan 19 '18 at 11:02

1 Answers1

0

You image link url is correct i have checked. so may be it is problem with Arbitrary Load permission

So to solve this problem add below Key value to your info.plist file

<key>NSAppTransportSecurity</key>
 <dict>
      <key>NSAllowsArbitraryLoads</key>
     <true/>
 </dict>
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40