react-native-blob-util icon indicating copy to clipboard operation
react-native-blob-util copied to clipboard

support downloading file in background on iOS

Open truongnguyen3 opened this issue 7 months ago • 5 comments

Motivation the URLSession has one function that allows us to download files in background execution (correct me if I am wrong). Here's the document I found in Xcode: image

Details When downloading files, apps should use an NSURLSession object to start the downloads so that the system can take control of the download process in case the app is suspended or terminated. When you configure an NSURLSession object for background transfers, the system manages those transfers in a separate process and reports status back to your app in the usual way. If your app is terminated while transfers are ongoing, the system continues the transfers in the background and launches your app (as appropriate) when the transfers finish or when one or more tasks need your app’s attention.

To support background transfers, you must configure your NSURLSession object appropriately. To configure the session, you must first create a NSURLSessionConfiguration object and set several properties to appropriate values. You then pass that configuration object to the appropriate initialization method of NSURLSession when creating your session.

The process for creating a configuration object that supports background downloads is as follows:

Create the configuration object using the backgroundSessionConfigurationWithIdentifier: method of NSURLSessionConfiguration. Set the value of the configuration object’s sessionSendsLaunchEvents property to YES. if your app starts transfers while it is in the foreground, it is recommend that you also set the discretionary property of the configuration object to YES. Configure any other properties of the configuration object as appropriate. Use the configuration object to create your NSURLSession object. Once configured, your NSURLSession object seamlessly hands off upload and download tasks to the system at appropriate times. If tasks finish while your app is still running (either in the foreground or the background), the session object notifies its delegate in the usual way. If tasks have not yet finished and the system terminates your app, the system automatically continues managing the tasks in the background. 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) and calls the app delegate’s application:handleEventsForBackgroundURLSession:completionHandler: method. (The system may also relaunch the app to handle authentication challenges or other task-related events that require your app’s attention.) In your implementation of that delegate method, use the provided identifier to create a new NSURLSessionConfiguration and NSURLSession object with the same configuration as before. The system reconnects your new session object to the previous tasks and reports their status to the session object’s delegate.

refs:

  • https://github.com/alpha0010/react-native-file-access/issues/79
  • https://github.com/itinance/react-native-fs/issues/1213

truongnguyen3 avatar Dec 05 '23 03:12 truongnguyen3

any solutions ?

crackhack8266 avatar Dec 06 '23 14:12 crackhack8266

@crackhack8266 we need someone to work on it. I did create another issue in https://github.com/edeckers/react-native-blob-courier/issues/251 the package react-native-blob-courier focuses on download and upload tasks, So it just needs a little change to support download tasks on background execution in iOS

truongnguyen3 avatar Dec 07 '23 07:12 truongnguyen3

Thank you for the input. It sure would be nice to have and probably change the whole ios implementation. But currently I don't have the amount of free time to implement this

RonRadtke avatar Dec 07 '23 07:12 RonRadtke

Thanks for your reply. I am currently learning iOS native development, maybe I will contribute to your repo later if I can.

truongnguyen3 avatar Dec 07 '23 08:12 truongnguyen3

Thanks for your reply. I am currently learning iOS native development, maybe I will contribute to your repo later if I can.

Would be great. How is it going? Any way I can help you getting going with iOS native development?

RonRadtke avatar Apr 07 '24 12:04 RonRadtke