NSURLSessionDownloadTask 继续下载,即使我杀死了应用程序

NSURLSessionDownloadTask continue downloading even if I kill the App

我正在使用 NSURLSessionDownloadTask 下载网页文件。 当我使用[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:]创建NSURLSessionConfiguration时,我发现NSURLSessionDownloadTask即使我杀死了应用程序也继续下载。 这不是我所期望的,我只是希望它在后台下载。我不想终止应用程序,它仍在下载中。

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];

希望我杀掉app后,app不再执行下载任务

我已经大致了解了。 在苹果文档中看到了[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:]的描述:

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.

因为我使用 xcode 处的关闭按钮来关闭并重新启动我的应用程序,苹果文档是这样说的:"iOS app is terminated by the system and relaunched."当应用程序以这种方式关闭时,NSURLSessionDownloadTask不会结束。 如果用户从多任务屏幕关闭应用程序,系统将取消所有会话的后台传输。

那么我的问题就解决了!