2017-11-02 4 views
0

Ich muss eine Datei mit meiner Desktop-Anwendung herunterladen. Zuerst habe ich ein einfaches iOS-Projekt zum Herunterladen einer Videodatei erstellt, das sich auf diese web site bezieht. Es klappt. Und ich möchte so ziemlich dasselbe für Mac OS machen.Herunterladen von Datei mit NSURLSessionDownloadTask

class ViewController: NSViewController, URLSessionDownloadDelegate { 
    var downloadTask: URLSessionDownloadTask! 
    var backgroundSession: URLSession! 
    var assetFile = String() // file in application's sandboxed folder 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundSession") 
     backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main) 
    } 

    override func viewDidAppear() { 
     super.viewDidAppear() 
     let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")! 
     downloadTask = backgroundSession.downloadTask(with: url) 
     downloadTask.resume() 
    } 

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) { 
     let fileManager = FileManager() 
     do { 
      let assetURL = URL(fileURLWithPath: assetFile) 
      try fileManager.moveItem(at: location, to: assetURL) 
     } catch { 
      print("An error occurred while moving file to destination url") 
     } 
    } 

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) { 

    } 

    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { 
     downloadTask = nil 
     if (error != nil) { 
      print(error!.localizedDescription) // <<< error here? 
     } else { 
      print("The task finished transferring data successfully") 
     } 
    } 
} 

Wenn ich es ausführen, erhalte ich eine Protokollmeldung, die "unbekannter Fehler." Ich frage mich, was ich falsch mache? Ich habe App Transport Security Setting>Allow Allow Arbitrary Loads auf YES festgelegt. Vielen Dank.

Antwort

0

Da Ihre App Sandboxed ist, stellen Sie sicher, dass Outgoing Connections in den Sandbox-Funktionen aktiviert sind.

enter image description here

Die ATS-Einstellungen haben keine Auswirkungen für eine HTTPS-Verbindung