Swift3 从URLSessionDownloadTask恢复数据始终为零
我有一个应用程序,当我下载文件时,我需要从互联网上下载文件。它工作正常,但我的问题是,当我按下暂停按钮暂停下载一分钟或更长时间时,我从简历数据中得到零 以下是我的代码:Swift3 从URLSessionDownloadTask恢复数据始终为零,swift3,nsurlsessiondownloadtask,Swift3,Nsurlsessiondownloadtask,我有一个应用程序,当我下载文件时,我需要从互联网上下载文件。它工作正常,但我的问题是,当我按下暂停按钮暂停下载一分钟或更长时间时,我从简历数据中得到零 以下是我的代码: @IBAction func startDownloading(_ sender: UIButton) { isDownload = true sessionConfig = URLSessionConfiguration.default let operationQueue = Operati
@IBAction func startDownloading(_ sender: UIButton)
{
isDownload = true
sessionConfig = URLSessionConfiguration.default
let operationQueue = OperationQueue.main
session = URLSession.init(configuration: sessionConfig, delegate: self, delegateQueue: operationQueue)
let url = URL(string: "www.example.com")
downloadTask = session.downloadTask(with: url!)
downloadTask.resume()
}
@IBAction func pause(_ sender: UIButton)
{
if downloadTask != nil && isDownload
{
self.downloadTask!.cancel(byProducingResumeData: { (resumeData) in
// here is the nil from the resume data
})
isDownload = false
downloadTask = nil
pasueBtnOutlet.setTitle("Resume", for: .normal)
}
if !isDownload && downloadData != nil
{
downloadTask = session.downloadTask(withResumeData: downloadData as Data)
downloadTask.resume()
isDownload = true
downloadData = nil
pasueBtnOutlet.setTitle("Pause", for: .normal)
}
}
请你能帮我吗
谢谢大家您的代码似乎是正确的,您只需在结尾处用
resumeData
初始化downloadData
将属性设置为downloadData
var downloadData:Data!
self.downloadTask!.cancel(byProducingResumeData: { (resumeData) in
// here is the nil from the resume data
// You have to set download data with resume data
self.downloadData = resumeData
})
然后在您取消任务的暂停按钮操作中,使用resumeData
设置downloadData
var downloadData:Data!
self.downloadTask!.cancel(byProducingResumeData: { (resumeData) in
// here is the nil from the resume data
// You have to set download data with resume data
self.downloadData = resumeData
})
为了检查进度和完成情况,请执行这些URLSessionDownloadDelegate
委托
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
print(Int(progress * 100))
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Downloading done")
}
注意:-尝试一个有效的可下载url。例如
它是http,请确保在Info.plist中设置传输安全性。您的代码似乎是正确的,您只需在闭包中使用
resumeData
初始化downloadData
将属性设置为downloadData
var downloadData:Data!
self.downloadTask!.cancel(byProducingResumeData: { (resumeData) in
// here is the nil from the resume data
// You have to set download data with resume data
self.downloadData = resumeData
})
然后在您取消任务的暂停按钮操作中,使用resumeData
设置downloadData
var downloadData:Data!
self.downloadTask!.cancel(byProducingResumeData: { (resumeData) in
// here is the nil from the resume data
// You have to set download data with resume data
self.downloadData = resumeData
})
为了检查进度和完成情况,请执行这些URLSessionDownloadDelegate
委托
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
print(Int(progress * 100))
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Downloading done")
}
注意:-尝试一个有效的可下载url。例如
它的http,,请确保在Info.plist中设置传输安全。问题是resumeData是关闭时的零。您可以尝试此操作,您将看到它是零。我认为应用程序中存在问题。您的URL应该对下载有效。您要下载的URL是什么?请提供URLfirst@HaiderAhmed您是否检查了浏览器上的URL是否正确播放歌曲或下载歌曲问题是resumeData是关闭时的零。您可以尝试此操作,您会看到它是零。我认为应用程序中存在问题。您的URL应该对下载有效。您要下载的URL是什么?请提供URLfirst@HaiderAhmed您是否检查了浏览器上的URL是否正在播放这首歌或为你下载它可能的副本这是可能的副本这是可能的副本