2

I am using the NSURLSessionDownloadTask to download a web file. When I use [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:] to create NSURLSessionConfiguration, I found NSURLSessionDownloadTask continue downloading even if I kill the App. This is not what I expect, I just want it download in the background. I don't want to kill the app, it's still being downloaded.

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"DDDownloader"];
config.timeoutIntervalForRequest = 10;
self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:downloadModel.url]];
[downloadTask resume];

I hope that after I kill the app, the app will no longer perform the download task.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Eddie Wu
  • 61
  • 6
  • 1
    Your requirement is not clear what you want ? – vivekDas Apr 15 '19 at 07:31
  • When App killed you have some time provided by the OS to complete something pending. You may use `applicationWillTerminate` to pause or cancel downloading task – Prashant Tukadiya Apr 15 '19 at 07:33
  • I want NSURLSessionDownloadTask not to continue downloading when I kill my application. – Eddie Wu Apr 15 '19 at 07:36
  • 1
    @wuqh How do you know that URLSession keep downloading if you kill your app ? – Prashant Tukadiya Apr 15 '19 at 07:37
  • @PrashantTukadiya Here's my code that you can test :https://github.com/wqhiOS/DDDownloader. Download a file, kill the app when half of it is downloaded, then open the app later, you will find that the download has been completed – Eddie Wu Apr 15 '19 at 07:49
  • @PrashantTukadiya The reason keep downloading is because I use it 、`[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:]`,Use `[NSURLSessionConfiguration defaultSessionConfiguration]` and it won't happen again. But I don't want to use` [NSURLSessionConfiguration defaultSessionConfiguration]` – Eddie Wu Apr 15 '19 at 07:58
  • Use dataTask instead. – Cy-4AH Apr 15 '19 at 09:04

1 Answers1

1

I have got the general idea now. I saw the description of [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:] in the apple document:

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 to 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. In addition, the system does not automatically relaunch apps that were force quit by the user. The user must explicitly relaunch the app before transfers can begin again.

Because I'm using the close button at xcode to close and restart my app, this is what the apple document says: "iOS app is terminated by the system and relaunched." When an app is closed in this way, the NSURLSessionDownloadTask will not end. If the user close the app from the multitasking screen, the system cancels all of the session’s background transfers.

So my problem is solved!

trungduc
  • 11,926
  • 4
  • 31
  • 55
Eddie Wu
  • 61
  • 6