1

i have an app which i need to download the file from the internet when i downloading the file it's work good but my problem is when i pressed pause button to pause the downloading for one minute or more i get nil from resume Data

the following my code :

@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)
    }                    
}

please can help me

thanks for all

Naresh Reddy M
  • 1,096
  • 1
  • 10
  • 27
Haider Ahmed
  • 169
  • 1
  • 2
  • 12
  • Possible duplicate of [NSURLSessionDownloadTask cancelByProducingResumeData return null](https://stackoverflow.com/questions/24215610/nsurlsessiondownloadtask-cancelbyproducingresumedata-return-null) – Nikolay Shubenkov Jun 29 '17 at 08:28
  • This is dublicate of https://stackoverflow.com/questions/24215610/nsurlsessiondownloadtask-cancelbyproducingresumedata-return-null – Nikolay Shubenkov Jun 29 '17 at 08:29

1 Answers1

0

Your code seems to be correct, you just need to init the downloadData with resumeData in closure

Make a property of downloadData

var downloadData:Data!

and then in your pause button action where you cancel the task, set the downloadData with resumeData

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
})

In order to check the progress and completion, implement these URLSessionDownloadDelegate delegates

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")
}

NOTE:- Try a valid downloadable url. For example http://www.star.uclan.ac.uk/news_and_events/news/2010020901/sdo_resolution_comparison.png

Its http, make sure to set Transport Security in Info.plist

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98