2323
2424@implementation RNFetchBlobNetwork
2525
26+ NSOperationQueue *taskQueue;
2627
2728@synthesize taskId;
2829@synthesize expectedBytes;
@@ -31,10 +32,17 @@ @implementation RNFetchBlobNetwork
3132@synthesize callback;
3233@synthesize bridge;
3334@synthesize options;
35+ @synthesize fileTaskCompletionHandler;
36+ @synthesize dataTaskCompletionHandler;
37+ @synthesize error;
38+
3439
3540// constructor
3641- (id )init {
3742 self = [super init ];
43+ if (taskQueue == nil ) {
44+ taskQueue = [[NSOperationQueue alloc ] init ];
45+ }
3846 return self;
3947}
4048
@@ -51,7 +59,8 @@ + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
5159}
5260
5361// 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+ {
5564 self.taskId = taskId;
5665 self.respData = [[NSMutableData alloc ] initWithLength: 0 ];
5766 self.callback = callback;
@@ -62,12 +71,15 @@ - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskI
6271
6372 NSString * path = [self .options valueForKey: CONFIG_FILE_PATH];
6473 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];
6778
6879 // file will be stored at a specific path
6980 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) {
7183 if (error != nil ) {
7284 callback (@[[error localizedDescription ]]);
7385 return ;
@@ -81,12 +93,14 @@ - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskI
8193 return ;
8294 }
8395 callback (@[[NSNull null ], path]);
84- }];
96+ };
97+ NSURLSessionDownloadTask * task = [session downloadTaskWithRequest: req completionHandler: fileTaskCompletionHandler];
8598 [task resume ];
8699 }
87100 // file will be stored at tmp path
88101 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) {
90104 if (error != nil ) {
91105 callback (@[[error localizedDescription ]]);
92106 return ;
@@ -101,22 +115,22 @@ - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskI
101115 return ;
102116 }
103117 callback (@[[NSNull null ], tmpPath]);
104- }];
118+ };
119+ NSURLSessionDownloadTask * task = [session downloadTaskWithRequest: req completionHandler: fileTaskCompletionHandler];
105120 [task resume ];
106121 }
107122 // base64 response
108123 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) {
112125 if (error != nil ) {
113126 callback (@[[error localizedDescription ]]);
114127 return ;
115128 }
116129 else {
117130 callback (@[[NSNull null ], [resp base64EncodedStringWithOptions: 0 ]]);
118131 }
119- }];
132+ };
133+ NSURLSessionDataTask * task = [session dataTaskWithRequest: req completionHandler: dataTaskCompletionHandler];
120134 [task resume ];
121135 }
122136}
@@ -160,6 +174,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
160174
161175- (void ) URLSession : (NSURLSession *)session task : (NSURLSessionTask *)task didCompleteWithError : (NSError *)error {
162176 NSLog ([error localizedDescription ]);
177+ self.error = error;
163178}
164179
165180// upload progress handler
@@ -177,4 +192,28 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
177192 ];
178193}
179194
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+
180219@end
0 commit comments