Search code examples
iosobjective-ciphonensurlsessionnsurlsessiondownloadtask

on iOS 7+, how to force a download operation to be run by the system rather than the app?


I am wanting to test background downloads of large files in iOS 7+ applications.

I am using a NSURLSession with a background configuration as explained in the Apple documentation on Background Execution.

The documentation states that "Once configured, your NSURLSession object seamlessly hands off upload and download tasks to the system at appropriate times."

I am trying to for the creation of such appropriate times so that I can test an app before release. Various examples suggest that I can simply bring a different application to the foreground to trigger handover of the background download task to the system.

However, that is not proving to be the case. Instead, the download completes under the control of the app.

Importantly

- (void)application:(UIApplication *)application
handleEventsForBackgroundURLSession:(NSString *)identifier
  completionHandler:(void (^)(void))completionHandler

never runs in my application delegate.

I have tried to force a handover on both the iOS simulator and on actual devices but with no luck.

Thoughts on ways to force handover to of background downloads to the system would be appreciated.


Solution

  • There is no "handover". Every task in a background session always runs in a separate process, period.

    The application:handleEventsForBackgroundURLSession:completionHandler: method, however, runs only if your app gets terminated by the OS (not by the user) while a download is in progress. If this happens, your app gets relaunched in the background whenever the download finishes.

    You can potentially test this by starting a large download in the background that will take long enough to complete that your app will get evicted... or by switching to another app that pigs out on RAM until your app gets evicted.