NSURLSession: handleEventsForBackgroundURLSession method doesn't run in Background session

Viewed 3149

application:handleEventsForBackgroundURLSession:completionHandler: doesn't run when the uploads are finished in the background. When all the upload/download tasks are completed in the background then iOS should wake up the app and call the above method. But it doesn't.

  1. I have tested this by terminating the app from 'xcode stop' button and also terminated from an exception.
  2. I have followed Background Transfer consideration.
  3. I have tested this on Apple developer guide: Background example and it is not working.
  4. I have also tested this on AppCoda: Background example and it is not working
  5. Both of the examples which are given in 2 and 3 points are not working on iOS 9.0 and xCode 7.0. Before upgrading to iOS9.0, I had tested this on iOS7.0 and xcode 5.0 where it ran perfectly.
  6. I am assuming that it would work on the real device.

What am I missing here? Can anybody confirm whether handleEventsForBackgroundURLSession works in iOS9.0 simulator or am I doing anything wrong? Since it doesn't work for the examples given in 2 and 3 so I guess maybe I am testing it wrong.

2 Answers

In my case, the cause of this was that I was using uploadTask(withStreamedRequest:...), which is not compatible with background URL sessions. I switched to use uploadTask(with:request, fromFile:...) instead and everything started working.

Related