Swift 2.0 not downloading images to UI ImageView

1.2k views Asked by At

Team,

Weird problem. I took the plunge into Swift 2. So far it's pretty great and while I've had to do some code fixup, it hasn't been too bad.

Then I hit this weird bug (I think it's a bug?)

I can't seem to download images to a UIImage View. I've tried a few different approaches. But none seem to work. Has anyone been able to get this to work (populating image views from a URL) in Swift 2?

Here's my latest try:

//
//  ViewController.swift
//  uiimage_from_url


import UIKit

class ViewController: UIViewController {

    @IBOutlet var image_element: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        load_image("http://s3.evcdn.com/images/block250/I0-001/016/377/706-0.jpeg_/jazz-cam-graduation-2015-06.jpeg")

    }

    func load_image(urlString:String)
    {

        let imgURL: NSURL = NSURL(string: urlString)!
        let request: NSURLRequest = NSURLRequest(URL: imgURL)
        let response:NSURLResponse = NSURLResponse()
        let data:NSData = NSData()
        let error:NSError
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ response, data, error in /* Your code */ })
        self.image_element.image = UIImage(data: data)
    }


}
1

There are 1 answers

0
Davemen On BEST ANSWER

So after being downvoted for the question, I assumed I was going to have to answer it myself. Thanks down voters :(

Apparently iOS 9 and I'm guessing OSX 10.11 requires TLSv1.2 SSL for all hosts that you plan to request data from. Unless.... you specify exception domains in your application's Info.plist file.

The easy/cheap fix is to do something like this:

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

I'm sure there's a better/cleaner way. But this will re-enable downloading any image from any website.