应用程序强制退出并重新启动后,如何恢复NSURLSession下载过程? [英] How to resume NSURLSession download process after app force-quit and app relaunch?

查看:152
本文介绍了应用程序强制退出并重新启动后,如何恢复NSURLSession下载过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了NSURLSession,用于从我们的服务器下载相当大的文件.现在,只要我在前台或后台工作,然后返回到应用程序,交易就可以正常进行并完成.

I have implemented NSURLSession for downloading fairly large files from our servers. Now as long as I'm working in foreground or background and go back to the app the transactions are working and getting finished.

但是,如果我使用多任务屏幕强制退出该应用程序,然后再次重新打开该应用程序.下载过程尚未完成,尽管据我了解,文档应该显示以下内容:

But if I force-quit the app using the the multitasking screen and re-open the app again. the downloading process is not getting finished although as I understood from the docs, it should, here what the docs state:

如果iOS应用被系统终止并重新启动,则该应用可以使用相同的标识符来创建新的配置对象和会话,并检索终止时正在进行的传输状态.此行为仅适用于系统正常终止应用程序的情况.如果用户从多任务屏幕中终止该应用程序,则系统会取消所有会话的后台传输.此外,系统不会自动重新启动由用户强制退出的应用程序. 用户必须明确重新启动该应用,然后才能再次开始传输.

意思是如果我重新启动应用程序,则应该在强制退出之前重新开始交易,或者是吗?我需要执行其他操作才能使其正常工作吗?

Meaning if I relaunch the app the transaction before the force-quit should started again, or are they? Is there an additional operation I need to commit in order this to work?

更新:我偶然发现了这个项目: https://github.com/Heikowi/HWIFileDownload#force-quit

UPDATE: I stumbled on this project: https://github.com/Heikowi/HWIFileDownload#force-quit

说明:

强制退出

应用被用户杀死后,下载不会在后台继续.在iOS 7(及更高版本)上,恢复数据被传回.

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编写的,我不知道他们在做什么以实现这一目标.

Meaning there is a way to receive resume data even if the application was killed by the user in the background. Only the project is written in Objective-C and I can't understand what are they doing to achieve this.

推荐答案

强制退出后:

 NSURLSessionTaskDelegate - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

重新启动应用程序时,将调用

delegate方法.如果可以恢复下载任务,则错误对象将包含恢复数据:

delegate method will be called when the app is restarted. If the download task can be resumed the error object will contain the resume data:

[error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData].

使用这些数据,您可以通过创建NSURLSessionDownloadTask来恢复下载过程:

Using this data you can resume the download process by creating a NSURLSessionDownloadTask with:

(NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData*)resumeData.

有关此内容的更多信息,请参见

More info about that can be found in Life Cycle of a URL Session with Custom Delegates, step 13.

这篇关于应用程序强制退出并重新启动后,如何恢复NSURLSession下载过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆