如何在应用程序强制退出和应用程序重新启动后恢复 NSURLSession 下载过程?
How to resume NSURLSession download process after app force-quit and app relaunch?
我已经实施 NSURLSession
从我们的服务器下载相当大的文件。现在,只要我在前台或后台工作并返回应用程序,交易就会正常进行并完成。
但是如果我强制退出 使用多任务屏幕的应用程序并再次重新打开应用程序。下载过程没有完成,但正如我从文档中了解到的那样,它应该是这样的,这里是文档状态:
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. 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.
意思是如果我重新启动应用程序,那么 force-quit 之前的交易应该会再次开始,是吗?我是否需要执行其他操作才能使其正常工作?
更新: 我偶然发现了这个项目:
https://github.com/Heikowi/HWIFileDownload#force-quit
即:
Force Quit
After the app has been killed by the user, downloads do not continue in the background. On iOS 7 (and later) resume data is passed back.
这意味着即使应用程序在后台被用户终止,也有一种方法可以接收恢复数据。只有项目是用Objective-C写的,我不明白他们在做什么来实现这个。
我认为在您的应用程序强制退出后您应该重新开始 (.
If the user terminates your app, the system cancels any pending tasks.
和
When all of the tasks associated with a background session are complete, the system relaunches a terminated app (assuming that the sessionSendsLaunchEvents property was set to YES and that the user did not force quit the app)
强制退出后:
NSURLSessionTaskDelegate - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
委托方法将在应用程序重新启动时被调用。如果可以恢复下载任务,错误对象将包含恢复数据:
[error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData]
.
使用此数据,您可以通过创建 NSURLSessionDownloadTask
恢复下载过程:
(NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData*)resumeData.
可以在 Life Cycle of a URL Session with Custom Delegates 的第 13 步中找到更多相关信息。
-> 使用 URLSession 后台会话下载根本不会停止....您不必明确编码以恢复下载或其他内容..
https://developer.apple.com/reference/foundation/urlsession
检查此 link 中的后台会话...如果您无法获得静止图像...评论我,我会提供详细帮助。
我已经实施 NSURLSession
从我们的服务器下载相当大的文件。现在,只要我在前台或后台工作并返回应用程序,交易就会正常进行并完成。
但是如果我强制退出 使用多任务屏幕的应用程序并再次重新打开应用程序。下载过程没有完成,但正如我从文档中了解到的那样,它应该是这样的,这里是文档状态:
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. 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.
意思是如果我重新启动应用程序,那么 force-quit 之前的交易应该会再次开始,是吗?我是否需要执行其他操作才能使其正常工作?
更新: 我偶然发现了这个项目: https://github.com/Heikowi/HWIFileDownload#force-quit
即:
Force Quit
After the app has been killed by the user, downloads do not continue in the background. On iOS 7 (and later) resume data is passed back.
这意味着即使应用程序在后台被用户终止,也有一种方法可以接收恢复数据。只有项目是用Objective-C写的,我不明白他们在做什么来实现这个。
我认为在您的应用程序强制退出后您应该重新开始 (.
If the user terminates your app, the system cancels any pending tasks.
和
When all of the tasks associated with a background session are complete, the system relaunches a terminated app (assuming that the sessionSendsLaunchEvents property was set to YES and that the user did not force quit the app)
强制退出后:
NSURLSessionTaskDelegate - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
委托方法将在应用程序重新启动时被调用。如果可以恢复下载任务,错误对象将包含恢复数据:
[error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData]
.
使用此数据,您可以通过创建 NSURLSessionDownloadTask
恢复下载过程:
(NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData*)resumeData.
可以在 Life Cycle of a URL Session with Custom Delegates 的第 13 步中找到更多相关信息。
-> 使用 URLSession 后台会话下载根本不会停止....您不必明确编码以恢复下载或其他内容..
https://developer.apple.com/reference/foundation/urlsession
检查此 link 中的后台会话...如果您无法获得静止图像...评论我,我会提供详细帮助。