Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cbpanchal opened this issue Aug 27, 2019 · 12 comments

Comments

@cbpanchal
Copy link

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
Copy link
Author

@joltup

@cbpanchal
Copy link
Author

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

@TfADrama
Copy link

Someone knows a library that works on background for ios?

@venkataluri
Copy link

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
Copy link

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) })

@ramazanarslan
Copy link

@venkataluri thanks it worked really. @mustafakemalelma

@ankitpmi
Copy link

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!!!

@DodlaRajasekharReddy1995
Copy link

DodlaRajasekharReddy1995 commented Jun 6, 2022

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.

@hamidpak013
Copy link

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

@OrtizJorge97
Copy link

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!

@giapmn-1380
Copy link

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.

@maksymhcode-care
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants