Another way to resume if download.cancel(byProducingResumeData: ..) not working

1.2k views Asked by At

Hi I followed this excellent tutorial on how to download (and resume-download) files to an iPhone.

Everything works, except that the service-provider I am downloading the files from somehow does not allow for download-resume to work.

Apple says that a download can be resumed only if the following conditions are met (see bullets below). Therefore my dwonload.cancel(byProducingResmeData: ..) possibly returns data = nil because of one of these points :

  • The resource has not changed since you first requested it
  • The task is an HTTP or HTTPS GET request
  • The server provides either the ETag or Last-Modified header (or both) in its response
  • The server supports byte-range requests
  • The temporary file hasn’t been deleted by the system in response to disk space pressure

At least one of the above points unfortunately seems to not be fulfilled by my service-provider (definitive answer from them hanging....).

Anyway - is there another way to resume a background-downloading Task in iOS ?

If I could persistently keep the URLSessionDownloadTask - this would help. But how would I do that ?

Here is my code that normally keeps the resumeData. But unfortunately, in my case, this data is always nil.

func pauseDownload(_ file: File) {
    guard let download = activeDownloads[file.previewURL] else { return }
    if download.isDownloading {
        download.task?.cancel(byProducingResumeData: { data in
          download.resumeData = data
          // data unfortunately always nil !!!!!!!!!!!!!!!!!!!!! 
        })
        download.isDownloading = false
    }
}
0

There are 0 answers