Skip to content

Commit 58444c7

Browse files
hakonkfacebook-github-bot
authored andcommitted
Custom NSURLSession configuration (#27701)
Summary: While it is possible in the React Native implementation for Android to provide a custom configuration for HTTP requests, the iOS implementation does not allow for the same customization. As the NSURLSession used for HTTP requests on iOS is configured internally, one may for instance not supply an ephemeral configuration for HTTP requests. Other concerns related to the given problem have been addressed in the community: react-native-community/discussions-and-proposals#166. I did make a PR with an RFC in the community repo, but after some discussion in the said repo, I figured I might as well make a PR with a suggestion :) ## Changelog [iOS] [Added] - Allow for configuring the NSURLSessionConfiguration Implement a C function `RCTSetCustomNSURLSessionConfigurationProvider` which gives the app programmer the ability to provide a block which provides an NSURLSessionConfiguration that will be used for all HTTP requests instead of the default configuration. The provided block will be called when the session configuration is needed. Pull Request resolved: #27701 Test Plan: Unsure if this can be tested in any other way than uncommenting the example code in `RNTester/RNTester/AppDelegate.mm`. Reviewed By: yungsters Differential Revision: D28680384 Pulled By: JoshuaGross fbshipit-source-id: ae24399955581a1cc9f4202f0f6f497bfe067a5c
1 parent 0d32aef commit 58444c7

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

Libraries/Network/RCTHTTPRequestHandler.h

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#import <React/RCTInvalidating.h>
99
#import <React/RCTURLRequestHandler.h>
1010

11+
typedef NSURLSessionConfiguration* (^NSURLSessionConfigurationProvider)(void);
12+
/**
13+
* The block provided via this function will provide the NSURLSessionConfiguration for all HTTP requests made by the app.
14+
*/
15+
RCT_EXTERN void RCTSetCustomNSURLSessionConfigurationProvider(NSURLSessionConfigurationProvider);
1116
/**
1217
* This is the default RCTURLRequestHandler implementation for HTTP requests.
1318
*/

Libraries/Network/RCTHTTPRequestHandler.mm

+19-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ @interface RCTHTTPRequestHandler () <NSURLSessionDataDelegate, RCTTurboModule>
1818

1919
@end
2020

21+
static NSURLSessionConfigurationProvider urlSessionConfigurationProvider;
22+
23+
void RCTSetCustomNSURLSessionConfigurationProvider(NSURLSessionConfigurationProvider provider) {
24+
urlSessionConfigurationProvider = provider;
25+
}
26+
2127
@implementation RCTHTTPRequestHandler
2228
{
2329
NSMapTable *_delegates;
@@ -75,14 +81,20 @@ - (NSURLSessionDataTask *)sendRequest:(NSURLRequest *)request
7581
NSOperationQueue *callbackQueue = [NSOperationQueue new];
7682
callbackQueue.maxConcurrentOperationCount = 1;
7783
callbackQueue.underlyingQueue = [[_moduleRegistry moduleForName:"Networking"] methodQueue];
78-
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
79-
// Set allowsCellularAccess to NO ONLY if key ReactNetworkForceWifiOnly exists AND its value is YES
80-
if (useWifiOnly) {
81-
configuration.allowsCellularAccess = ![useWifiOnly boolValue];
84+
NSURLSessionConfiguration *configuration;
85+
if (urlSessionConfigurationProvider) {
86+
configuration = urlSessionConfigurationProvider();
87+
} else {
88+
configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
89+
// Set allowsCellularAccess to NO ONLY if key ReactNetworkForceWifiOnly exists AND its value is YES
90+
if (useWifiOnly) {
91+
configuration.allowsCellularAccess = ![useWifiOnly boolValue];
92+
}
93+
[configuration setHTTPShouldSetCookies:YES];
94+
[configuration setHTTPCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
95+
[configuration setHTTPCookieStorage:[NSHTTPCookieStorage sharedHTTPCookieStorage]];
8296
}
83-
[configuration setHTTPShouldSetCookies:YES];
84-
[configuration setHTTPCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
85-
[configuration setHTTPCookieStorage:[NSHTTPCookieStorage sharedHTTPCookieStorage]];
97+
assert(configuration != nil);
8698
_session = [NSURLSession sessionWithConfiguration:configuration
8799
delegate:self
88100
delegateQueue:callbackQueue];

0 commit comments

Comments
 (0)