Friday, August 31, 2018

Will download resume after closing my app in Background Mode

Leave a Comment

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.

Is it possible to resume downloading task after force closing my application by user? How?

3 Answers

Answers 1

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!

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.

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.

After calling this method, the app also posts a UIApplicationWillTerminate notification to give interested objects a chance to respond to the transition.

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

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.

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

Answers 3

Yes—if I understood your need right—Apple allows this with State Preservation and Restoration APIs:

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.

Details about preservation process can be found in article: About the UI Preservation Process

Details about restoration process can be found here: About the UI Restoration Process

Raywenderlich have—a little outdated—tutorial implementation of this framework @ State Restoration Tutorial: Getting Started

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment