This repository has been archived by the owner on Nov 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
RNFileDownload.m
43 lines (33 loc) · 1.69 KB
/
RNFileDownload.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// RNFileDownload.m
// RNFileDownload
//
// Created by Perry Poon on 8/31/15.
// Copyright (c) 2015 Perry Poon. All rights reserved.
//
#import "RNFileDownload.h"
#import "RNFileDownloadSessionManager.h"
@implementation RNFileDownload
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(download:(NSString *)source targetPath:(NSString *)targetPath downloadFileName:(NSString *)fileName headers:(NSDictionary *)headers callback:(RCTResponseSenderBlock)callback) {
if(!source)
callback(@[@"source need to be not null"]);
if(!targetPath)
callback(@[@"targetPath need to be not null"]);
NSURL *URL = [NSURL URLWithString:source];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
for (NSString *key in headers.allKeys){
[request setValue:headers[key] forHTTPHeaderField:key];
}
RNFileDownloadSessionManager *manager = [[RNFileDownloadSessionManager alloc] initWithTargetPath:targetPath
downloadFileName:fileName
bridge:self.bridge
callback:callback];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSession sharedSession].delegate
delegate:manager
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
[downloadTask resume];
}
@end