Unable to load http URL Image

I'm trying to convert a watchOS 1 app to watchOS 2.

But I saw an error when trying to load an image from an URL with this code:



            let url:NSURL = NSURL(string: "http://www.xxxxxxxx.com/xxx/xyz.jpg")!
            let data:NSData = try! NSData(contentsOfURL:url, options:[])
            imageone.setImage(UIImage(data: data))




The error is:


> Error Domain=NSCocoaErrorDomain Code=256 "The file “xyz.jpg” couldn’t be opened." UserInfo={NSURL=http://www.xxxxxxxx.com/xxx/xyz.jpg}



I read on the internet that is because of the new Application Transport Security protocol.

I also read to put



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



in the info.plist on the Watchkit extension but the same error appear.

It's the same also if I use an image in an HTTPS domain.



NOTE: On the simulator it works perfectly!



I'm using XCode 7 beta 5.



Anyone knows how to solve?



Thanks

It's the same on XCode 7 beta 6

I had a similar problem when downloading from the internet, where it worked on the simulator but not on the device.


Switching to using NSURLSession fixed that for me.

Thanks, I'll try and let you know!

Can you please help me to convert my code to use NSURLSession?

Accepted Answer

I solved creating a function:



func imageRequest(urlweb:NSURL) {

let requestURL: NSURL = urlweb

let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)

let session = NSURLSession.sharedSession()

let task = session.dataTaskWithRequest(urlRequest) {

(data, response, error) -> Void in

if error == nil {

NSLog("Success!")

self.imagename?.setImage(UIImage(data:data!))

} else {

NSLog("Fail")

}

}

task.resume()

}

Unable to load http URL Image
 
 
Q