rn-fetch-blob icon indicating copy to clipboard operation
rn-fetch-blob copied to clipboard

how to download files in background for iOS ?? IOSBackgroundTask: true not work for me, any one please help me out ??

Open cbpanchal opened this issue 5 years ago • 12 comments

Hi ! Thank you for reporting an issue, but we would like to remind you, we have a trouble shooting page in our wiki. You may want to take a look on that page or find issues tagged "trouble shooting" :p

  • please provide the version of installed library and RN project.
  • a sample code snippet/repository is very helpful to spotting the problem.
  • issues which have been tagged as 'needs feedback', will be closed after 2 weeks if receive no feedbacks.
  • issues lack of detailed information will be closed without any feedback

cbpanchal avatar Aug 27 '19 11:08 cbpanchal

@joltup

cbpanchal avatar Aug 27 '19 11:08 cbpanchal

I am done with this https://github.com/Jacse/rn-fetch-blob#ios-background-uploading but still not working, please check if anyone has an idea @joltup

Thanks

cbpanchal avatar Aug 28 '19 13:08 cbpanchal

Someone knows a library that works on background for ios?

TfADrama avatar Dec 17 '19 15:12 TfADrama

Hi, I am also looking for IOS background upload support, here is my code RNFetchBlob.config({ IOSBackgroundTask: true, }) .fetch('POST', videoPath, { 'ClientToken': access_token, 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(fileUrl)) .then((res) => { console.log('res',res.text()) }) .catch((err) => { console.log('err',err) })

while uploading data to server, if application is moved to background and again brining back to foreground RNFetchBlob callbacks are not getting called and showing loading continuously.

can anyone help me to fix this issue

Thanks

venkataluri avatar Mar 23 '20 10:03 venkataluri

Hi, after adding the following code in AppDelegate, i am able to upload file in background

//AppDelegate.h
@property (nonatomic, assign) UIBackgroundTaskIdentifier taskIdentifier;

// AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application {
    if (self.taskIdentifier != UIBackgroundTaskInvalid) {
        [application endBackgroundTask:self.taskIdentifier];
        self.taskIdentifier = UIBackgroundTaskInvalid;
    }
    
    __weak typeof(self) weakSelf = self;
    self.taskIdentifier = [application beginBackgroundTaskWithName:nil expirationHandler:^{
        [application endBackgroundTask:weakSelf.taskIdentifier];
        weakSelf.taskIdentifier = UIBackgroundTaskInvalid;
    }];
}

here is my RNFetchBlob code RNFetchBlob.config({ IOSBackgroundTask: true, }) .fetch('POST', videoPath, { 'ClientToken': access_token, 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(fileUrl)) .then((res) => { console.log('res',res.text()) }) .catch((err) => { console.log('err',err) })

venkataluri avatar Mar 28 '20 06:03 venkataluri

@venkataluri thanks it worked really. @mustafakemalelma

ramazanarslan avatar Apr 21 '20 13:04 ramazanarslan

Hi, after adding the following code in AppDelegate, i am able to upload file in background

//AppDelegate.h
@property (nonatomic, assign) UIBackgroundTaskIdentifier taskIdentifier;

// AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application {
    if (self.taskIdentifier != UIBackgroundTaskInvalid) {
        [application endBackgroundTask:self.taskIdentifier];
        self.taskIdentifier = UIBackgroundTaskInvalid;
    }
    
    __weak typeof(self) weakSelf = self;
    self.taskIdentifier = [application beginBackgroundTaskWithName:nil expirationHandler:^{
        [application endBackgroundTask:weakSelf.taskIdentifier];
        weakSelf.taskIdentifier = UIBackgroundTaskInvalid;
    }];
}

here is my RNFetchBlob code RNFetchBlob.config({ IOSBackgroundTask: true, }) .fetch('POST', videoPath, { 'ClientToken': access_token, 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(fileUrl)) .then((res) => { console.log('res',res.text()) }) .catch((err) => { console.log('err',err) })

Thanks, It worked for me!!!

ankitpmi avatar May 25 '22 05:05 ankitpmi

Hi, after adding the following code in AppDelegate, i am able to upload file in background

//AppDelegate.h
@property (nonatomic, assign) UIBackgroundTaskIdentifier taskIdentifier;

// AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application {
    if (self.taskIdentifier != UIBackgroundTaskInvalid) {
        [application endBackgroundTask:self.taskIdentifier];
        self.taskIdentifier = UIBackgroundTaskInvalid;
    }
    
    __weak typeof(self) weakSelf = self;
    self.taskIdentifier = [application beginBackgroundTaskWithName:nil expirationHandler:^{
        [application endBackgroundTask:weakSelf.taskIdentifier];
        weakSelf.taskIdentifier = UIBackgroundTaskInvalid;
    }];
}

here is my RNFetchBlob code RNFetchBlob.config({ IOSBackgroundTask: true, }) .fetch('POST', videoPath, { 'ClientToken': access_token, 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(fileUrl)) .then((res) => { console.log('res',res.text()) }) .catch((err) => { console.log('err',err) })

We are trying to download a video file in ios and got this error when the app went to background,

we have used the above code provided by @venkataluri , the download is working fine till 30 sec after that we are getting same issue, can someone help out here... we are stuck with this issue.

Someone knows a library that works on background for ios? npm i react-native-background-fetch try this one

hamidpak013 avatar May 24 '23 06:05 hamidpak013

It worked for me, but with a little of modifications in my AppDelegate.mm (note that for me it is not AppDelegate.m)

//AppDelegate.mm
- (void)applicationWillResignActive:(UIApplication *)application {
   if (self.taskIdentifier != UIBackgroundTaskInvalid) {
      [application endBackgroundTask:self.taskIdentifier];
      self.taskIdentifier = UIBackgroundTaskInvalid;
   }
 
   __weak AppDelegate *weakSelf = self; //THIS LINE IS DIFFERENT
   self.taskIdentifier = [application beginBackgroundTaskWithName:nil expirationHandler:^{
      if (weakSelf) {
          [application endBackgroundTask:weakSelf.taskIdentifier];
          weakSelf.taskIdentifier = UIBackgroundTaskInvalid;
      }
   }];
}

When tried to build with npx react-native run-ios --simulator="iPhone 15 Pro" command, with the previous solution it was not able to compile, so had to modify it the above way to work!

OrtizJorge97 avatar Oct 16 '23 03:10 OrtizJorge97

Hi, after adding the following code in AppDelegate, i am able to upload file in background

//AppDelegate.h @property (nonatomic, assign) UIBackgroundTaskIdentifier taskIdentifier;

// AppDelegate.m

  • (void)applicationWillResignActive:(UIApplication *)application { if (self.taskIdentifier != UIBackgroundTaskInvalid) { [application endBackgroundTask:self.taskIdentifier]; self.taskIdentifier = UIBackgroundTaskInvalid; }

    __weak typeof(self) weakSelf = self; self.taskIdentifier = [application beginBackgroundTaskWithName:nil expirationHandler:^{ [application endBackgroundTask:weakSelf.taskIdentifier]; weakSelf.taskIdentifier = UIBackgroundTaskInvalid; }]; } here is my RNFetchBlob code RNFetchBlob.config({ IOSBackgroundTask: true, }) .fetch('POST', videoPath, { 'ClientToken': access_token, 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(fileUrl)) .then((res) => { console.log('res',res.text()) }) .catch((err) => { console.log('err',err) })

=> sometime, download file error still happend. Message error "The network connection was lost" when app running in background.

giapmn-1380 avatar May 16 '24 07:05 giapmn-1380

Hi, after adding the following code in AppDelegate, i am able to upload file in background //AppDelegate.h @Property (nonatomic, assign) UIBackgroundTaskIdentifier taskIdentifier; // AppDelegate.m

  • (void)applicationWillResignActive:(UIApplication *)application { if (self.taskIdentifier != UIBackgroundTaskInvalid) { [application endBackgroundTask:self.taskIdentifier]; self.taskIdentifier = UIBackgroundTaskInvalid; } __weak typeof(self) weakSelf = self; self.taskIdentifier = [application beginBackgroundTaskWithName:nil expirationHandler:^{ [application endBackgroundTask:weakSelf.taskIdentifier]; weakSelf.taskIdentifier = UIBackgroundTaskInvalid; }]; } here is my RNFetchBlob code RNFetchBlob.config({ IOSBackgroundTask: true, }) .fetch('POST', videoPath, { 'ClientToken': access_token, 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(fileUrl)) .then((res) => { console.log('res',res.text()) }) .catch((err) => { console.log('err',err) })

=> sometime, download file error still happend. Message error "The network connection was lost" when app running in background.

Have you found a solution?

maksymhcode-care avatar Jul 30 '24 10:07 maksymhcode-care