How download audio files in react native iOS even after kill and relaunch the app?

Viewed 359

I have a scenario in my app which is developed using React Native (0.59), I am trying to download audio files. I want to continue download even after user kills and relaunch it. I am trying with 'react-native-background-downloader' package. I am download and play in simulator even after kill and relaunch the app. But not in actual device.Any suggestion would be appreciated.

Here is the code block I am using

let task = RNBackgroundDownloader.download({
    id: fileName,
    url: fileUrl,
    destination: `${RNBackgroundDownloader.directories.documents}/${fileName}`,
    
  }).begin((expectedBytes) => {
    console.log(`Going to download ${expectedBytes} bytes!`);
  }).progress((percent) => {
    console.log(`Downloaded: ${percent * 100}%`);
  }).done(() => {
    console.log('Download is done!');
  }).error((error) => {
    console.log('Download canceled due to error: ', error);
  });  

Here is my resume task

 let lostTasks = await RNBackgroundDownloader.checkForExistingDownloads();
  for (let task of lostTasks) {
    console.log(`Task ${task.id} was found!`);
    task.progress((percent) => {
      console.log(`Re-Downloaded: ${percent * 100}%`);
    }).done(() => {
      console.log('Re-Downlaod is done!');
    }).error((error) => {
      console.log('Re-Download canceled due to error: ', error);
    });
  }

I am able to see Re-Downloaded: 99%, then I am getting error like 'Re-Download canceled due to error: ', '“CFNetworkDownload_jtLLac.tmp” couldn’t be moved to “Documents” because either the former doesn’t exist, or the folder containing the latter doesn’t exist.

1 Answers

iOS - Extra Mandatory Step

In your AppDelegate.m add the following code:

..
#import <RNBackgroundDownloader.h>

...

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
{
  [RNBackgroundDownloader setCompletionHandlerWithIdentifier:identifier completionHandler:completionHandler];
}

...

Failing to add this code will result in canceled background downloads.

and only use RNBackgroundDownloader.directories.documents for destination.

Related