diff --git a/Libraries/Network/RCTHTTPRequestHandler.h b/Libraries/Network/RCTHTTPRequestHandler.h index 75b84eb1224354..5a91bd0a71fa3a 100644 --- a/Libraries/Network/RCTHTTPRequestHandler.h +++ b/Libraries/Network/RCTHTTPRequestHandler.h @@ -8,6 +8,11 @@ #import #import +typedef NSURLSessionConfiguration* (^NSURLSessionConfigurationProvider)(void); +/** + * The block provided via this function will provide the NSURLSessionConfiguration for all HTTP requests made by the app. +*/ +RCT_EXTERN void RCTSetCustomNSURLSessionConfigurationProvider(NSURLSessionConfigurationProvider); /** * This is the default RCTURLRequestHandler implementation for HTTP requests. */ diff --git a/Libraries/Network/RCTHTTPRequestHandler.mm b/Libraries/Network/RCTHTTPRequestHandler.mm index b7f524395a77c6..d160a9e72f9e4f 100644 --- a/Libraries/Network/RCTHTTPRequestHandler.mm +++ b/Libraries/Network/RCTHTTPRequestHandler.mm @@ -18,6 +18,12 @@ @interface RCTHTTPRequestHandler () @end +static NSURLSessionConfigurationProvider urlSessionConfigurationProvider; + +void RCTSetCustomNSURLSessionConfigurationProvider(NSURLSessionConfigurationProvider provider) { + urlSessionConfigurationProvider = provider; +} + @implementation RCTHTTPRequestHandler { NSMapTable *_delegates; @@ -75,14 +81,20 @@ - (NSURLSessionDataTask *)sendRequest:(NSURLRequest *)request NSOperationQueue *callbackQueue = [NSOperationQueue new]; callbackQueue.maxConcurrentOperationCount = 1; callbackQueue.underlyingQueue = [[_moduleRegistry moduleForName:"Networking"] methodQueue]; - NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; - // Set allowsCellularAccess to NO ONLY if key ReactNetworkForceWifiOnly exists AND its value is YES - if (useWifiOnly) { - configuration.allowsCellularAccess = ![useWifiOnly boolValue]; + NSURLSessionConfiguration *configuration; + if (urlSessionConfigurationProvider) { + configuration = urlSessionConfigurationProvider(); + } else { + configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; + // Set allowsCellularAccess to NO ONLY if key ReactNetworkForceWifiOnly exists AND its value is YES + if (useWifiOnly) { + configuration.allowsCellularAccess = ![useWifiOnly boolValue]; + } + [configuration setHTTPShouldSetCookies:YES]; + [configuration setHTTPCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; + [configuration setHTTPCookieStorage:[NSHTTPCookieStorage sharedHTTPCookieStorage]]; } - [configuration setHTTPShouldSetCookies:YES]; - [configuration setHTTPCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; - [configuration setHTTPCookieStorage:[NSHTTPCookieStorage sharedHTTPCookieStorage]]; + assert(configuration != nil); _session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:callbackQueue];