0

When my application is running in the foreground I see periodic calls to: NSURLSessionDownloadDelegate -> URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:

When my application moves to the background and is suspended, calls to this method stop so I have no feedback on whether NSURLSessionDownloadTask is continuing to download. Are there any techniques out there for getting some feedback about the download?

drbobdugan
  • 443
  • 5
  • 12

1 Answers1

1

From looking at other StackOverflow posts about NSURLSessionDownloadTask there is a lot of confusion. Here are the feedback mechanisms I uncovered:

  • use a network proxy and packet sniffer like Wireshark (see this for details). Filter out all traffic except traffic to/from the server you are downloading the file from. Look for FIN,ACK to signal that the download is complete. You can also use the packet sniffer to diagnose problems with the download. Wireshark

Wireshark

  • use Instruments to capture network traffic for the process nsurlsessiond

Instruments capture for nsurlsessiond

  • use xcode Window->Devices->Download Container to download the app from the phone during the background download. You will find a cached version of the partially downloaded file in: /Library/Caches/com.apple.nsurlsessiond/Downloads/[id]/CFNetworkDownload_...tmp where [id] is the unique id that you supplied when you created your NSURLSessionConfiguration object. The cached version grows in size during the background download, so you can download the container multiple times to verify that the download is progressing. Once the download completes, the cached version is removed.

  • wait for the download to complete. If your application is in the background and suspended when the download completes, then iOS moves your application from the suspended state to actively running in the background and invokes the following methods in this order:

  • via UIApplicationDelegate -> handleEventsForBackgroundURLSession in AppDelegate which triggers the invocation of a completionHandler() method

  • via 'NSURLSessionDownloadTaskDelegate -> URLSession:downloadTask:didFinishDownloadingToURL:'
  • via NSURLSessionTaskDelegate -> NSURLSession:task:didCompleteWithError:
  • via NSURLSessionDelegate -> :URLSessionDidFinishEventsForBackgroundURLSession:
  • iOS gives the application 30 seconds TOTAL to handle end of download in the background before suspending the application

I've also put a very basic NSURLSessionDownload example application on github here.

Community
  • 1
  • 1
drbobdugan
  • 443
  • 5
  • 12