1

I want to download an image from Facebook and store it to my cache, which I call over HTTPS. If I just use HTTP everything works fine, but if I change it to HTTPS it does not work anymore.

Here is my code:

// Grab the artworkUrl key to get an image URL for thumbnail
        var urlString: NSString = rowData["cover"] as NSString

        // Check our image cache for the existing key. This is just a dictionary of UIImages
        var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage

        if( !image? ) {
            // If the image does not exist, we need to download it
            var imgURL: NSURL = NSURL(string: urlString)

            // Download an NSData representation of the image at the URL
            var request: NSURLRequest = NSURLRequest(URL: imgURL)

            var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)

            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in

                if !error? {

                    //var imgData: NSData = NSData(contentsOfURL: imgURL)
                    image = UIImage(data: data)

                    // Store the image in to our cache
                    self.imageCache.setValue(image, forKey: urlString)

                    cell.image = image


                    println(self.imageCache)

                }
                else {
                    println("Error: \(error.localizedDescription)")
                }
            })

        }
        else {
            cell.image = image
        }


    })

The URL I want to use is this: https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xpf1/t1.0-9/q71/s720x720/995054_485489274919674_7207866955460529362_n.jpg

The Error is a "Timeout".

With this URL everything works fine: http://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xpf1/t1.0-9/q71/s720x720/995054_485489274919674_7207866955460529362_n.jpg

Thanks, Tobias

Tobias
  • 578
  • 1
  • 5
  • 23

1 Answers1

2

From what I can tell NSURLConnection sometimes runs into trouble with HTTPS connections. Try adding these two methods to your class (and mark it as NSURLConnectionDelegate) (from this answer):

func connection(connection: NSURLConnection!, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace!) -> Bool  {
    return true
}

func connection(connection: NSURLConnection!, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge!) {
    challenge.sender.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust), forAuthenticationChallenge: challenge)
}
Community
  • 1
  • 1
Nate Cook
  • 92,417
  • 32
  • 217
  • 178