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.
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!