I figured out about it is possible to download in background mode of application. I have implemented Background Fetching Mode in XCode and registered background task and its working fine.
我想到可以在后台应用程序模式下载。我在XCode中实现了后台提取模式,并注册了后台任务,并且工作正常。
Is it possible to resume downloading task after force closing my application by user? How?
用户强制关闭我的应用程序后是否可以恢复下载任务?怎么样?
3 个解决方案
#1
5
No, you can't continue download when your app get terminated by user! Your app must require to remains in background state!!! Because if user force close app that means, he doesn't want to run it anymore. If your app is suspended by system then it can be wake up but not if it's terminated by user!
不,当您的应用被用户终止时,您无法继续下载!您的应用必须要求保持背景状态!因为如果用户强制关闭app意味着,他不想再运行它了。如果您的应用程序被系统暂停,那么它可以被唤醒,但如果它被用户终止则不会被唤醒!
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.
如果iOS应用程序被系统终止并重新启动,则应用程序可以使用相同的标识符来创建新的配置对象和会话,并检索终止时正在进行的传输的状态。此行为仅适用于系统正常终止应用程序。如果用户从多任务处理屏幕终止应用程序,系统将取消所有会话的后台传输。此外,系统不会自动重新启动用户强制退出的应用程序。在再次开始传输之前,用户必须明确重新启动应用程序。
Update : (As asked in comment)
更新:(如评论中所述)
Refer the apple documentation, It states,
请参阅苹果文档,它说,
This method lets your app know that it is about to be terminated and purged from memory entirely. You should use this method to perform any final clean-up tasks for your app, such as freeing shared resources, saving user data, and invalidating timers. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.
此方法可让您的应用知道它将被终止并完全从内存中清除。您应该使用此方法为应用程序执行任何最终清理任务,例如释放共享资源,保存用户数据以及使计时器无效。您执行此方法大约需要五秒钟来执行任何任务并返回。如果方法在时间到期之前没有返回,则系统可能完全终止该过程。
For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.
对于不支持后台执行或与iOS 3.x或更早版本链接的应用程序,当用户退出应用程序时,始终会调用此方法。对于支持后台执行的应用程序,当用户退出应用程序时通常不会调用此方法,因为在这种情况下应用程序只是移动到后台。但是,可以在应用程序在后台运行(未暂停)并且系统由于某种原因需要终止它的情况下调用此方法。
After calling this method, the app also posts a UIApplicationWillTerminate notification to give interested objects a chance to respond to the transition.
调用此方法后,该应用程序还会发布UIApplicationWillTerminate通知,以便为感兴趣的对象提供响应转换的机会。
#2
2
When any task completes, the NSURLSession object calls the delegate’s URLSession:task:didCompleteWithError: method with either an error object or nil (if the task completed successfully). If the task failed, most apps should retry the request until either the user cancels the download or the server returns an error indicating that the request will never succeed. Your app should not retry immediately, however. Instead, it should use reachability APIs to determine whether the server is reachable, and should make a new request only when it receives a notification that reachability has changed.
当任何任务完成时,NSURLSession对象使用错误对象或nil调用委托的URLSession:task:didCompleteWithError:方法(如果任务成功完成)。如果任务失败,大多数应用程序应重试请求,直到用户取消下载或服务器返回错误,指示请求永远不会成功。但是,您的应用不应立即重试。相反,它应该使用可访问性API来确定服务器是否可访问,并且只有在收到可访问性已更改的通知时才应发出新请求。
If the download task can be resumed, the NSError object’s userInfo dictionary contains a value for the NSURLSessionDownloadTaskResumeData key. Your app should pass this value to call downloadTaskWithResumeData: or downloadTaskWithResumeData:completionHandler: to create a new download task that continues the existing download.
如果可以恢复下载任务,则NSError对象的userInfo字典包含NSURLSessionDownloadTaskResumeData键的值。您的应用程序应传递此值以调用downloadTaskWithResumeData:或downloadTaskWithResumeData:completionHandler:创建一个继续现有下载的新下载任务。
If the task cannot be resumed, your app should create a new download task and restart the transaction from the beginning.
如果无法恢复任务,您的应用程序应创建新的下载任务并从头开始重新启动事务。
checkout here: Life cycle of URL Session
checkout here:URL Session的生命周期
#3
1
Yes—if I understood your need right—Apple allows this with State Preservation and Restoration APIs:
是的 - 如果我理解您的需要 - Apple允许使用State Preservation and Restoration API:
Return your app to its previous state after it is terminated by the system.
系统终止后,将应用程序恢复到之前的状态。
Check Apple's article: Preserving Your App's UI Across Launches, for an overview of this framework.
查看Apple的文章:在启动时保留应用程序的UI,以获取此框架的概述。
Details about preservation process can be found in article: About the UI Preservation Process
有关保存过程的详细信息,请参阅文章:关于UI保留过程
Details about restoration process can be found here: About the UI Restoration Process
有关恢复过程的详细信息,请参见此处:关于UI恢复过程
Raywenderlich have—a little outdated—tutorial implementation of this framework @ State Restoration Tutorial: Getting Started
Raywenderlich有一个有点过时的教程实现这个框架@ State Restoration Tutorial:Getting Started
#1
5
No, you can't continue download when your app get terminated by user! Your app must require to remains in background state!!! Because if user force close app that means, he doesn't want to run it anymore. If your app is suspended by system then it can be wake up but not if it's terminated by user!
不,当您的应用被用户终止时,您无法继续下载!您的应用必须要求保持背景状态!因为如果用户强制关闭app意味着,他不想再运行它了。如果您的应用程序被系统暂停,那么它可以被唤醒,但如果它被用户终止则不会被唤醒!
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.
如果iOS应用程序被系统终止并重新启动,则应用程序可以使用相同的标识符来创建新的配置对象和会话,并检索终止时正在进行的传输的状态。此行为仅适用于系统正常终止应用程序。如果用户从多任务处理屏幕终止应用程序,系统将取消所有会话的后台传输。此外,系统不会自动重新启动用户强制退出的应用程序。在再次开始传输之前,用户必须明确重新启动应用程序。
Update : (As asked in comment)
更新:(如评论中所述)
Refer the apple documentation, It states,
请参阅苹果文档,它说,
This method lets your app know that it is about to be terminated and purged from memory entirely. You should use this method to perform any final clean-up tasks for your app, such as freeing shared resources, saving user data, and invalidating timers. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.
此方法可让您的应用知道它将被终止并完全从内存中清除。您应该使用此方法为应用程序执行任何最终清理任务,例如释放共享资源,保存用户数据以及使计时器无效。您执行此方法大约需要五秒钟来执行任何任务并返回。如果方法在时间到期之前没有返回,则系统可能完全终止该过程。
For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.
对于不支持后台执行或与iOS 3.x或更早版本链接的应用程序,当用户退出应用程序时,始终会调用此方法。对于支持后台执行的应用程序,当用户退出应用程序时通常不会调用此方法,因为在这种情况下应用程序只是移动到后台。但是,可以在应用程序在后台运行(未暂停)并且系统由于某种原因需要终止它的情况下调用此方法。
After calling this method, the app also posts a UIApplicationWillTerminate notification to give interested objects a chance to respond to the transition.
调用此方法后,该应用程序还会发布UIApplicationWillTerminate通知,以便为感兴趣的对象提供响应转换的机会。
#2
2
When any task completes, the NSURLSession object calls the delegate’s URLSession:task:didCompleteWithError: method with either an error object or nil (if the task completed successfully). If the task failed, most apps should retry the request until either the user cancels the download or the server returns an error indicating that the request will never succeed. Your app should not retry immediately, however. Instead, it should use reachability APIs to determine whether the server is reachable, and should make a new request only when it receives a notification that reachability has changed.
当任何任务完成时,NSURLSession对象使用错误对象或nil调用委托的URLSession:task:didCompleteWithError:方法(如果任务成功完成)。如果任务失败,大多数应用程序应重试请求,直到用户取消下载或服务器返回错误,指示请求永远不会成功。但是,您的应用不应立即重试。相反,它应该使用可访问性API来确定服务器是否可访问,并且只有在收到可访问性已更改的通知时才应发出新请求。
If the download task can be resumed, the NSError object’s userInfo dictionary contains a value for the NSURLSessionDownloadTaskResumeData key. Your app should pass this value to call downloadTaskWithResumeData: or downloadTaskWithResumeData:completionHandler: to create a new download task that continues the existing download.
如果可以恢复下载任务,则NSError对象的userInfo字典包含NSURLSessionDownloadTaskResumeData键的值。您的应用程序应传递此值以调用downloadTaskWithResumeData:或downloadTaskWithResumeData:completionHandler:创建一个继续现有下载的新下载任务。
If the task cannot be resumed, your app should create a new download task and restart the transaction from the beginning.
如果无法恢复任务,您的应用程序应创建新的下载任务并从头开始重新启动事务。
checkout here: Life cycle of URL Session
checkout here:URL Session的生命周期
#3
1
Yes—if I understood your need right—Apple allows this with State Preservation and Restoration APIs:
是的 - 如果我理解您的需要 - Apple允许使用State Preservation and Restoration API:
Return your app to its previous state after it is terminated by the system.
系统终止后,将应用程序恢复到之前的状态。
Check Apple's article: Preserving Your App's UI Across Launches, for an overview of this framework.
查看Apple的文章:在启动时保留应用程序的UI,以获取此框架的概述。
Details about preservation process can be found in article: About the UI Preservation Process
有关保存过程的详细信息,请参阅文章:关于UI保留过程
Details about restoration process can be found here: About the UI Restoration Process
有关恢复过程的详细信息,请参见此处:关于UI恢复过程
Raywenderlich have—a little outdated—tutorial implementation of this framework @ State Restoration Tutorial: Getting Started
Raywenderlich有一个有点过时的教程实现这个框架@ State Restoration Tutorial:Getting Started