2016-12-02 21 views
0
  • 這裏工作的代碼:NSURLSession - DownloadTask不能與某些URL

    let url = NSURL(string: "http://a337.phobos.apple.com/us/r30/Music/d5/a8/6b/mzi.msfqeogi.aac.p.m4a") 
    let downloadTask = self.downloadSession.downloadTaskWithURL(url!) 
    downloadTask.resume() 
    

- >確定

- >它做沒有工作;沒有任何反應

回答

1

默認情況下,從iOS 9開始,所有連接都必須是https。如果第二個網址不支持https://則下載失敗。

我建議您檢查您的錯誤代碼從下載回來

-2

請plist中

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
    <key>NSExceptionDomains</key> 
    <dict> 
     <key>akamaihd.net</key> 
     <dict> 
      <key>NSExceptionRequiresForwardSecrecy</key> 
      <false/> 
      <key>NSIncludesSubdomains</key> 
      <true/> 
     </dict> 
     <key>facebook.com</key> 
     <dict> 
      <key>NSExceptionAllowsInsecureHTTPLoads</key> 
      <true/> 
      <key>NSExceptionRequiresForwardSecrecy</key> 
      <false/> 
      <key>NSIncludesSubdomains</key> 
      <true/> 
     </dict> 
     <key>fbcdn.net</key> 
     <dict> 
      <key>NSExceptionRequiresForwardSecrecy</key> 
      <false/> 
      <key>NSIncludesSubdomains</key> 
      <true/> 
     </dict> 
     <key>graph.facebook.com</key> 
     <dict> 
      <key>NSExceptionAllowsInsecureHTTPLoads</key> 
      <true/> 
      <key>NSExceptionRequiresForwardSecrecy</key> 
      <false/> 
      <key>NSIncludesSubdomains</key> 
      <true/> 
     </dict> 
    </dict> 
</dict> 
</plist> 
然後使用Alamofire 4.0雨燕3.0

使用以下方法

添加,您可以下載任何東西,例如。 PDF

// Mark:- Download File 
    func DownloadFile(strFileUrl : String, strFileName : String, success:@escaping (_ strResultURL: String) -> Void , failure:@escaping (_ responseObject:AnyObject) -> Void) { 
     let destination: DownloadRequest.DownloadFileDestination = { _, _ in 
      let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] 
      let fileURL = documentsURL.appendingPathComponent("\(strFileName)") 

      return (fileURL, [.removePreviousFile, .createIntermediateDirectories]) 
     } 

     Alamofire.download(strFileUrl, to: destination).response { response in 

       if SVProgressHUD.isVisible() { 
        SVProgressHUD.dismiss() 
       } 

       if response.error == nil { 
        success("\(response.destinationURL!)") 
       } 
       else { 
        failure(response.error as AnyObject) 
       } 
      }.downloadProgress { (progress) in 
       print("Download Progress: \(progress.fractionCompleted)") 
       SVProgressHUD.showProgress(Float(progress.fractionCompleted), status: AppAlertMsg.KDownloadingPDFs) 
     } 
    } 
0

如果你想使用的URL與HTTP連接,然後在你的Info.plist文件添加以下 應用交通運輸安全設置,並在加款允許任意負載和值設置爲YES。

enter image description here