2017-09-15 59 views
1

嗨,下面的代碼可以從網絡服務器上下載圖片。該請求是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中,沒有問題。

我錯過了什麼?

+0

除了這個問題,您的代碼還有很多錯誤。不要使用'nil'檢查,而是使用'if let'語句。另外,你應該使用NSURLSession異步加載web請求。 – JAL

回答

1

NSURLErrorUnsupportedURL最可能的原因是該URL沒有可用的協議處理程序(也許您的URL字符串在URL前綴中缺少'http://')。

+1

問題在於:.addingPercentEncoding(withAllowedCharacters:.urlPathAllowed) –

0
Error : Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" 

-1002對於URL(主機名)而言,似乎更像是一個問題,而不是http。

0

我不是很確定,如果問題是由於iOS的11或9的Xcode,但聽起來像是你需要應用這個

.addingPercentEncoding(withAllowedCharacters:.urlPathAllowed)只有最後一部分

您可能會找到特殊字符的網址。我試了一下,它的工作原理,將它應用於完整的URL將http:格式應用於http%35,這不是url的基礎,因此不會檢索Web視圖的失敗事件。因此,僅將它應用於您可能懷疑它包含特殊字符的位置空間等的url部分,等等。