2

I have created background nsurlsession to perform download task. It worked well when the app was in background. However, download task seems to be canceled and failed when I closed the app (double click "Home" button and swipe up), and it made me to download from the beginning again when I relaunched the app. According to Apple document, background transfer works even the app is no longer running. Am I doing anything wrong?

LongNV
  • 892
  • 9
  • 21
  • Specify what you mean by "background transfer" because I believe you might be mistaking Apple's statement to one that's only relevant to `NSOperation` tasks. – jakenberg Jun 13 '14 at 18:36
  • It's not relevant to NSOperation. It's a feature of NSURLSession https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html – LongNV Jun 14 '14 at 00:43
  • As has [previously been discussed](http://stackoverflow.com/questions/25047427/does-nsurlsession-continue-file-transfer-if-the-app-is-killed-from-task-manager), iOS seems (sensibly) to take a force-quit as a hint that the user would like all app activity to stop, including background transfers. – Matt Gibson Nov 12 '14 at 09:35

3 Answers3

2

From the NSURLSessionConfiguration Class Reference:

If an iOS app is terminated by the system and relaunched, the app can use the same identifier to create a new configuration object and session and retrieve the status of transfers that were in progress at the time of termination. This behavior applies only for normal termination of the app by the system. If the user terminates the app from the multitasking screen, the system cancels all of the session’s background transfers.

So, while background transfers will continue if iOS itself closes your app during the normal course of things, if you force the quit from the multitasking screen, it will kill all your transfers.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
1

The app is not relaunched for background downloads when the user has force quit.

The iOS8 documentation for application:didReceiveRemoteNotification:fetchCompletionHandler: says:

Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a push notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77
0

In the first paragraph of NSURLSession documentation, we can observe:

This API provides a rich set of delegate methods for supporting authentication and gives your app the ability to perform background downloads when your app is not running or, in iOS, while your app is suspended.

Now notice where it states:

or, in iOS, while your app is suspended.

It looks like only OS X applications have the ability to finish background tasks while your app isn't running.

jakenberg
  • 2,125
  • 20
  • 38