added the ability to add header to the downloading request (#2)

This commit is contained in:
Elad Gil
2018-12-19 14:30:36 +02:00
parent 75213781fd
commit 2c7102cee5
6 changed files with 42 additions and 12 deletions
+11 -3
View File
@@ -109,13 +109,21 @@ RCT_EXPORT_METHOD(download: (NSDictionary *) options) {
NSString *identifier = options[@"id"];
NSString *url = options[@"url"];
NSString *destination = options[@"destination"];
NSDictionary *headers = options[@"headers"];
if (identifier == nil || url == nil || destination == nil) {
NSLog(@"[RNFileBackgroundDownload] - [Error] id, url and destination must be set");
NSLog(@"[RNBackgroundDownloader] - [Error] id, url and destination must be set");
return;
}
[self lazyInitSession];
NSURLSessionDownloadTask *task = [urlSession downloadTaskWithURL:[NSURL URLWithString:url]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
if (headers != nil) {
for (NSString *headerKey in headers) {
[request setValue:[headers valueForKey:headerKey] forHTTPHeaderField:headerKey];
}
}
NSURLSessionDownloadTask *task = [urlSession downloadTaskWithRequest:request];
RNBGDTaskConfig *taskConfig = [[RNBGDTaskConfig alloc] initWithDictionary: @{@"id": identifier, @"destination": destination}];
taskToConfigMap[task] = taskConfig;
idToTaskMap[identifier] = task;