@@ -86,6 +86,7 @@ @interface RNFetchBlobNetwork ()
8686 NSInteger respStatus;
8787 NSMutableArray * redirects;
8888 ResponseFormat responseFormat;
89+ BOOL * followRedirect;
8990}
9091
9192@end
@@ -214,6 +215,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
214215 self.expectedBytes = 0 ;
215216 self.receivedBytes = 0 ;
216217 self.options = options;
218+ followRedirect = [options valueForKey: @" followRedirect" ] == nil ? YES : [[options valueForKey: @" followRedirect" ] boolValue ];
217219 isIncrement = [options valueForKey: @" increment" ] == nil ? NO : [[options valueForKey: @" increment" ] boolValue ];
218220 redirects = [[NSMutableArray alloc ] init ];
219221 if (req.URL != nil )
@@ -236,8 +238,15 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
236238 bodyLength = contentLength;
237239
238240 // the session trust any SSL certification
239- // NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
240- NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: taskId];
241+ NSURLSessionConfiguration *defaultConfigObject;
242+ if (!followRedirect)
243+ {
244+ defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration ];
245+ }
246+ else
247+ {
248+ NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: taskId];
249+ }
241250
242251 // set request timeout
243252 float timeout = [options valueForKey: @" timeout" ] == nil ? -1 : [[options valueForKey: @" timeout" ] floatValue ];
@@ -275,7 +284,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
275284 respData = [[NSMutableData alloc ] init ];
276285 respFile = NO ;
277286 }
278-
287+
279288 __block NSURLSessionDataTask * task = [session dataTaskWithRequest: req];
280289 [taskTable setObject: task forKey: taskId];
281290 [task resume ];
@@ -284,15 +293,14 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
284293 if ([[options objectForKey: CONFIG_INDICATOR] boolValue ] == YES )
285294 [[UIApplication sharedApplication ] setNetworkActivityIndicatorVisible: YES ];
286295 __block UIApplication * app = [UIApplication sharedApplication ];
287-
296+
288297 // #115 handling task expired when application entering backgound for a long time
289298 UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName: taskId expirationHandler: ^{
290299 NSLog ([NSString stringWithFormat: @" session %@ expired" , taskId ]);
291300 [expirationTable setObject: task forKey: taskId];
292301 [app endBackgroundTask: tid];
293302 }];
294303
295-
296304}
297305
298306// #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
@@ -575,7 +583,7 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
575583 respStr = [respData base64EncodedStringWithOptions: 0 ];
576584 }
577585 }
578- }
586+ }
579587
580588
581589 callback (@[ errMsg, rnfbRespType, respStr]);
@@ -644,9 +652,17 @@ - (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)sessio
644652
645653- (void ) URLSession : (NSURLSession *)session task : (NSURLSessionTask *)task willPerformHTTPRedirection : (NSHTTPURLResponse *)response newRequest : (NSURLRequest *)request completionHandler : (void (^)(NSURLRequest * _Nullable))completionHandler
646654{
647- if (request.URL != nil )
648- [redirects addObject: [request.URL absoluteString ]];
649- completionHandler (request);
655+
656+ if (followRedirect)
657+ {
658+ if (request.URL != nil )
659+ [redirects addObject: [request.URL absoluteString ]];
660+ completionHandler (request);
661+ }
662+ else
663+ {
664+ completionHandler (nil );
665+ }
650666}
651667
652668@end
0 commit comments