Vydia / react-native-background-upload

Upload files in your React Native app even while it's backgrounded. Supports Android and iOS, including camera roll assets.
MIT License
723 stars 330 forks source link

Feature: support for iOS completion events while the app is in background (Updated) #207

Closed wehriam closed 3 years ago

wehriam commented 4 years ago

Notes

Merges the current master branch with PR #145 from @drash-course and recreates the NSURLSession after each URLSessionDidFinishEventsForBackgroundURLSession call. All notes below the line are from @drash-course #145.


Problem

Currently, the uploads tasks initiated by the app while in foreground continue to completion even if the app is backgrounded. Unfortunately, the app does not wake up to receive the URLSession:task:didCompleteWithError event until it is brought to foreground by the user. This means the app does not receive any events (e.g. "completed") until then.

What this PR does

This PR adds a setBackgroundSessionCompletionHandler setter in the library to let the AppDelegate set the background session completion handler when it receives the application:handleEventsForBackgroundURLSession:completionHandler event. The lib will then wait for a short time to let the JS side provide new tasks (if any) and call the handler to sleep again.

Here is a sample implementation for the AppDelegate:

#import "VydiaRNFileUploader.h"
// ...
- (void)application:(UIApplication *)application
        handleEventsForBackgroundURLSession:(NSString *)identifier
        completionHandler:(void (^)(void))completionHandler {
  [VydiaRNFileUploader setBackgroundSessionCompletionHandler:completionHandler];
}

There should be no regression if the AppDelegate does not implement this. Most apps don't create more tasks after completion so they won't need this addition.

Feel free to amend this PR or discuss the changes.

Install steps

$(SRCROOT)/../node_modules/react-native-background-upload/ios must be added to the header search paths to enable the AppDelegate to find "VydiaRNFileUploader.h". Do you know how to automate this with react-native link ?

reime005 commented 4 years ago

@wehriam thanks for that. if you need to adjust the header search paths, I think you can do this by editing the podspec. this might work, but I've not tested it:

s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)\" \"$(PODS_ROOT)/../../node_modules/react-native-background-upload/ios\"" }

possible that it is USER_HEADER_SEARCH_PATHS