I just made a class以在后台轻松下载文件。但是,当我打电话给FileDownloader.startDownload()
时,URLSessionDownloadTask
会失败(有时会立即失败,有时几秒钟之后),没有例外。 error?.localizedDescription
和error.debugDescription
均为nil
。如果我尝试使用代码given in Swift Developer Blog在主线程中下载,则不会发生此问题。有没有什么办法解决这一问题?这是我如何称呼课程:
let downloader = FileDownloader.init(downloadURL: URL(string: "http://link.to/a/file/on/the/internet")!, identifier: "com.example.myapp.downloader")
downloader.startDownload()
while downloader.downloadStatus == DownloadStatus.InProgress {}
if (downloader.downloadStatus == DownloadStatus.Completed) { /* stuff */ }
else { /* downloadStatus is DownloadStatus.Failed, which is always the case */ }
这是macOS命令行工具。
编辑:经过一些测试,看来我确实是向服务器发送了一个请求,因为task.response
显示了以下内容:
Optional(<NSHTTPURLResponse: 0x1021037f0> { URL: http://link.to/a/file/on/the/internet } { Status Code: 200, Headers {
"Accept-Ranges" = (
bytes
);
Connection = (
"Keep-Alive"
);
"Content-Length" = (
9463662
);
....