Alex Mensi Alex Mensi - 1 year ago 221
iOS Question

iOS 11 Loading Images from HTTP with ATS

Hi have the following code to download images from a web server. The request are HTTP so un secured.

DispatchQueue.global(qos: .userInitiated).async {
var productImage = Utilities.GetEmptyImageBottle()
if (entry.ImageName != nil || entry.ImageUrl != nil) {
let data = NSData(contentsOf: URL(string: entry.ImageUrl.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)!)

if data != nil {
let image = UIImage(data: data! as Data)
productImage = image!
}
}
DispatchQueue.main.async {
cell.ProductImage.image = productImage
}
}


I also added the following in my info.plist

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


Now my images don't load and I get the following error:

NSURLConnection finished with error - code -1002


This is in Xcode 9 running iOS 11. On iOS 10 in Xcode 8, no problems.

What did I missed?

Answer Source

The most likely cause of NSURLErrorUnsupportedURL is that there is no available protocol handler for the URL (maybe your URL string lacks 'http://' in the URL prefix?).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download