A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background.
On iOS, if you want to download big files no matter the state of your app, wether it's in the background or terminated by the OS, you have to use a system API called
This API handles your downloads separately from your app and only keeps it informed using delegates.
On Android we are simulating this process with a wonderful library called Fetch2
The real challenge of using this method is making sure the app's UI is always up-to-date with the downloads that are happening in another process because your app might startup from scratch while the downloads are still running.
react-native-background-downloader gives you an easy API to both downloading large files and re-attaching to those downloads once your app launches again.
$ yarn add react-native-background-downloader
RN <= 0.57.0 use
$ yarn add email@example.com
Mostly automatic installation
Any React Native version
>= 0.60 supports autolinking so nothing should be done.
< 0.60 run the following link command
$ react-native link react-native-background-downloader
- In XCode, in the project navigator, right click
Add Files to [your project's name]
- Go to
- In XCode, in the project navigator, select your project. Add
libRNBackgroundDownloader.ato your project's
Link Binary With Libraries
- Run your project (
- Open up
import com.eko.RNBackgroundDownloaderPackage;to the imports at the top of the file
new RNBackgroundDownloaderPackage()to the list returned by the
- Append the following lines to
include ':react-native-background-downloader' project(':react-native-background-downloader').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-downloader/android')
- Insert the following lines inside the dependencies block in
iOS - Extra Mandatory Step
AppDelegate.m add the following code:
......- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler...
Failing to add this code will result in canceled background downloads.
Downloading a file
;let task = RNBackgroundDownloader;// Pause the tasktask;// Resume after pausetask;// Cancel the tasktask;
Re-Attaching to background downloads
This is the main selling point of this library (but it's free!).
What happens to your downloads after the OS stopped your app? Well, they are still running, we just need to re-attach to them.
Add this code to app's init stage, and you'll never lose a download again!
;let lostTasks = await RNBackgroundDownloader;for let task of lostTasksconsole;task;
task.id is very important for re-attaching the download task with any UI component representing that task, this is why you need to make sure to give sensible IDs that you know what to do with, try to avoid using random IDs.
Using custom headers
If you need to send custom headers with your download request, you can do in it 2 ways:
- Globally using
This way, all downloads with have the given headers.
- Per download by passing a headers object in the options of
let task = RNBackgroundDownloader;
Headers given in the
download function are merged with the ones given in
Download a file to destination
An object containing options properties
||String||✅||All||A Unique ID to provide for this download. This ID will help to identify the download task when the app re-launches|
||String||✅||All||URL to file you want to download|
||String||✅||All||Where to copy the file to once the download is done|
||Object||All||Costume headers to add to the download request. These are merged with the headers given in the
||Priority (enum)||Android||The priority of the download. On Android, downloading is limited to 4 simultaneous instances where further downloads are queued. Priority helps in deciding which download to pick next from the queue. Default: Priority.MEDIUM|
||Network (enum)||Android||Give your the ability to limit the download to WIFI only. Default: Network.ALL|
DownloadTask - The download task to control and monitor this download
Checks for downloads that ran in background while you app was terminated. Recommended to run at the init stage of the app.
DownloadTask - Array of tasks that were running in the background so you can re-attach callbacks to them
Sets headers to use in all future downloads.
headers - Object
A class representing a download task created by
||String||The id you gave the task when calling
||Number||The current percent of completion of the task between 0 and 1|
||Number||The number of bytes currently written by the task|
||Number||The number bytes expected to be written by this task or more plainly, the file size being downloaded|
Use these methods to stay updated on what's happening with the task.
All callback methods return the current instance of the
DownloadTask for chaining.
||expectedBytes||Called when the first byte is received. 💡: this is good place to check if the device has enough storage space for this download|
||percent, bytesWritten, totalBytes||Called at max every 1.5s so you can update your progress bar accordingly|
||Called when the download is done, the file is at the destination you've set|
||error||Called when the download stops due to an error|
Pauses the download
Resumes a pause download
Stops the download for good and removes the file that was written so far
An absolute path to the app's documents directory. It is recommended that you use this path as the target of downloaded files.
Priority (enum) - Android only
Priority.MEDIUM - Default ✅
Network (enum) - Android only
Network.ALL - Default ✅