iOS 11使用ATS从HTTP加载图片
嗨,下面的代码可以从网络服务器上下载图片。该请求是HTTP如此不安全。iOS 11使用ATS从HTTP加载图片
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
}
}
我还添加了在我的info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
下现在我的图片不加载,我得到以下错误:
NSURLConnection finished with error - code -1002
这是在Xcode 9运行iOS 11.在Xcode 8的iOS 10中,没有问题。
我错过了什么?
NSURLErrorUnsupportedURL
最可能的原因是该URL没有可用的协议处理程序(也许您的URL字符串在URL前缀中缺少'http://')。
问题在于:.addingPercentEncoding(withAllowedCharacters:.urlPathAllowed) –
Error : Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
-1002对于URL(主机名)而言,似乎更像是一个问题,而不是http。
我不是很确定,如果问题是由于iOS的11或9的Xcode,但听起来像是你需要应用这个
.addingPercentEncoding(withAllowedCharacters:.urlPathAllowed)只有最后一部分
您可能会找到特殊字符的网址。我试了一下,它的工作原理,将它应用于完整的URL将http:格式应用于http%35,这不是url的基础,因此不会检索Web视图的失败事件。因此,仅将它应用于您可能怀疑它包含特殊字符的位置空间等的url部分,等等。
除了这个问题,您的代码还有很多错误。不要使用'nil'检查,而是使用'if let'语句。另外,你应该使用NSURLSession异步加载web请求。 – JAL