|
@@ -23,6 +23,7 @@
|
23
|
23
|
|
24
|
24
|
@implementation RNFetchBlobNetwork
|
25
|
25
|
|
|
26
|
+NSOperationQueue *taskQueue;
|
26
|
27
|
|
27
|
28
|
@synthesize taskId;
|
28
|
29
|
@synthesize expectedBytes;
|
|
@@ -31,10 +32,17 @@
|
31
|
32
|
@synthesize callback;
|
32
|
33
|
@synthesize bridge;
|
33
|
34
|
@synthesize options;
|
|
35
|
+@synthesize fileTaskCompletionHandler;
|
|
36
|
+@synthesize dataTaskCompletionHandler;
|
|
37
|
+@synthesize error;
|
|
38
|
+
|
34
|
39
|
|
35
|
40
|
// constructor
|
36
|
41
|
- (id)init {
|
37
|
42
|
self = [super init];
|
|
43
|
+ if(taskQueue == nil) {
|
|
44
|
+ taskQueue = [[NSOperationQueue alloc] init];
|
|
45
|
+ }
|
38
|
46
|
return self;
|
39
|
47
|
}
|
40
|
48
|
|
|
@@ -51,7 +59,8 @@
|
51
|
59
|
}
|
52
|
60
|
|
53
|
61
|
// send HTTP request
|
54
|
|
-- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req callback:(RCTResponseSenderBlock) callback {
|
|
62
|
+- (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback
|
|
63
|
+{
|
55
|
64
|
self.taskId = taskId;
|
56
|
65
|
self.respData = [[NSMutableData alloc] initWithLength:0];
|
57
|
66
|
self.callback = callback;
|
|
@@ -62,12 +71,15 @@
|
62
|
71
|
|
63
|
72
|
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
|
64
|
73
|
NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
|
65
|
|
-
|
66
|
|
- NSURLSession * session = [NSURLSession sharedSession];
|
|
74
|
+ NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
|
|
75
|
+ NSURLSession * session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
|
|
76
|
+
|
|
77
|
+// NSURLSession * session = [NSURLSession sharedSession];
|
67
|
78
|
|
68
|
79
|
// file will be stored at a specific path
|
69
|
80
|
if( path != nil) {
|
70
|
|
- NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
81
|
+
|
|
82
|
+ self.fileTaskCompletionHandler = ^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
71
|
83
|
if(error != nil) {
|
72
|
84
|
callback(@[[error localizedDescription]]);
|
73
|
85
|
return;
|
|
@@ -81,12 +93,14 @@
|
81
|
93
|
return;
|
82
|
94
|
}
|
83
|
95
|
callback(@[[NSNull null], path]);
|
84
|
|
- }];
|
|
96
|
+ };
|
|
97
|
+ NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
|
85
|
98
|
[task resume];
|
86
|
99
|
}
|
87
|
100
|
// file will be stored at tmp path
|
88
|
101
|
else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
|
89
|
|
- NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
102
|
+
|
|
103
|
+ self.fileTaskCompletionHandler = ^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
90
|
104
|
if(error != nil) {
|
91
|
105
|
callback(@[[error localizedDescription]]);
|
92
|
106
|
return;
|
|
@@ -101,14 +115,13 @@
|
101
|
115
|
return;
|
102
|
116
|
}
|
103
|
117
|
callback(@[[NSNull null], tmpPath]);
|
104
|
|
- }];
|
|
118
|
+ };
|
|
119
|
+ NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
|
105
|
120
|
[task resume];
|
106
|
121
|
}
|
107
|
122
|
// base64 response
|
108
|
123
|
else {
|
109
|
|
- NSURLSessionUploadTask * task =
|
110
|
|
-
|
111
|
|
- [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
124
|
+ self.dataTaskCompletionHandler = ^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
112
|
125
|
if(error != nil) {
|
113
|
126
|
callback(@[[error localizedDescription]]);
|
114
|
127
|
return;
|
|
@@ -116,7 +129,8 @@
|
116
|
129
|
else {
|
117
|
130
|
callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
|
118
|
131
|
}
|
119
|
|
- }];
|
|
132
|
+ };
|
|
133
|
+ NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:dataTaskCompletionHandler];
|
120
|
134
|
[task resume];
|
121
|
135
|
}
|
122
|
136
|
}
|
|
@@ -160,6 +174,7 @@
|
160
|
174
|
|
161
|
175
|
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
|
162
|
176
|
NSLog([error localizedDescription]);
|
|
177
|
+ self.error = error;
|
163
|
178
|
}
|
164
|
179
|
|
165
|
180
|
// upload progress handler
|
|
@@ -177,4 +192,28 @@
|
177
|
192
|
];
|
178
|
193
|
}
|
179
|
194
|
|
|
195
|
+- (void) application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
|
|
196
|
+
|
|
197
|
+}
|
|
198
|
+
|
|
199
|
+- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
|
|
200
|
+{
|
|
201
|
+ if(self.dataTaskCompletionHandler != nil)
|
|
202
|
+ {
|
|
203
|
+ dataTaskCompletionHandler(self.respData, nil, error);
|
|
204
|
+ }
|
|
205
|
+ else if(self.fileTaskCompletionHandler != nil)
|
|
206
|
+ {
|
|
207
|
+ fileTaskCompletionHandler(nil, nil, self.error);
|
|
208
|
+ }
|
|
209
|
+}
|
|
210
|
+
|
|
211
|
+- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
|
|
212
|
+{
|
|
213
|
+ if([options valueForKey:CONFIG_TRUSTY] == YES)
|
|
214
|
+ completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
|
|
215
|
+ else
|
|
216
|
+ RCTLogError(@"counld not create connection with an unstrusted SSL certification, if you're going to create connection anyway, add `trusty:true` to RNFetchBlob.config");
|
|
217
|
+}
|
|
218
|
+
|
180
|
219
|
@end
|