1

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
    }
}
iKK
  • 6,394
  • 10
  • 58
  • 131
  • Something that hit me, so worth asking: have you checked that when you call cancel the state of task is not "cancelling" or "complete"? This can happen for example if the session's invalidateAndCancel is called. Or maybe also during shutdown of the app? During shutdown of the app I handle this more appropriately taking the resume data from the error you receive in `urlSession(_:task:didCompleteWithError:)`. – Matteo Mecucci May 16 '18 at 11:48
  • No - I don't think the state of the task was "cancelling" or "complete"... I have another provider and with him the "resumeData" works perfectly. It is only with the first server where I have problems. There the data is unfortunately always nil. So it does seem to be a server-setting issue of the data-server providers one is dealing with ! – iKK May 16 '18 at 12:03
  • @iKK have yo done resume functionalities for both cases, App relaunch and an app is in memory? – Amrit Tiwari Nov 21 '18 at 04:05
  • @Amrit: Thanks for the hint. I have only tried App-relaunch since I only need this case. – iKK Nov 25 '18 at 20:06
  • @iKK can you please help me to download previous incomplete task? i have track it from urlSession(_:task:didCompleteWithError:) method but can not help to resume downloading task. – Amrit Tiwari Jan 04 '19 at 05:44
  • @Amrit: Do you have any code sample that you can share. It is difficult to grasp where the current issue in your code is... Helpful might be this article: [link](https://www.ralfebert.de/ios-examples/networking/urlsession-background-downloads/) – iKK Jan 05 '19 at 19:47
  • or this [link](https://realm.io/news/gwendolyn-weston-ios-background-networking/) – iKK Jan 05 '19 at 19:55
  • @iKK hello I made demo video where I am downloading the HLS video. I can't resume pending task after relaunch app. Demo url link : https://github.com/tiwariammit/HLSVideoDownloader – Amrit Tiwari Jan 10 '19 at 06:47
  • @Amrit: Please find a new file `feedback.txt` on your repo with some findings of mine and ideas of a workaround. In fact, your code is mostly working - it is just not showing the progress bar to update. The reason is unclear to me as well since I am far from being an expert. But my `feedback.txt` file contains some ideas that might bring your further and especially offers at least a workaround for a resume after your app went into background.(it does not offer a solution for a resume after you re-start your app. For this also, try another Server since sometimes, server-settings prevent resume) – iKK Jan 10 '19 at 17:07
  • 1
    I also noticed that the `urlAsset` is nil when you try to fill your downloadingMap from the persistence at startup of an app-restart (i.e. inside closure of `downloadSession.getAllTasks {...}` urlAsset is always nil). Maybe there is something wrong with your url-persistence. – iKK Jan 10 '19 at 17:20
  • @iKK I was busy sorry for the late response, and thank you for your suggestions. I have replaced my server URL by "https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8" but which is also not supported resume pending download task. I am confused about this issue whether due to a server or my codes. But work fine when downloading from crome. – Amrit Tiwari Jan 24 '19 at 09:50
  • @Amrit, I am sorry that I cannot help more here. I am confused about the resume-problem as well. Maybe take another app ([such as given in this tutorial](https://www.raywenderlich.com/567-urlsession-tutorial-getting-started)) and try changing to your server-url in order to see if it is your app or if it is your server that causes the resume-problem. Good luck ! – iKK Jan 24 '19 at 12:11
  • @iKK thank you for giving your precious time helping me. – Amrit Tiwari Jan 25 '19 at 04:05

0 Answers0