I would like to make an empty POST request and read the response headers while my iOS app is in the background. Looking at the documentation it explicitly states that Upload and Download tasks are supported in the background, but there are two problems:
Which leaves me with an NSURLSessionDataTask
, for which the documentation states:
Note: Prior to iOS 8 and OS X 10.10, data tasks are not supported in background sessions.
However, another documentation page states under the same title (Background Transfer Considerations):
Only upload and download tasks are supported (no data tasks).
Which documentation page is correct? Are NSURLSessionDataTasks without data to upload supported in background sessions?
See WWDC 2014 video What's New in Foundation Networking, about 49 minutes in. The bottom line is that you can perform data tasks in background sessions now, but it will only work only if the app is running. If app is suspended or terminated, it cannot perform data task, though you can convert it to a download task when it receives the response. (FWIW, I don't find it particularly useful to have a background data task that can only work while the app is still running.)
I'm not sure why you're worrying about download vs data task. It strikes me that you could just initiate a download task and then in didFinishDownloadingToURL
, look at downloadTask.response
.
Having said that I'm unclear what your broader intent is. If you want to ping your server (e.g. to see if data is available for download), you'd generally use background fetch for that.