Skip to content

Commit

Permalink
Custom NSURLSession configuration (#27701)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hakonk authored and tido64 committed Jun 8, 2021
1 parent 79ddbfb commit c299694
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Libraries/Network/RCTHTTPRequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#import <React/RCTInvalidating.h>
#import <React/RCTURLRequestHandler.h>

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.
*/
Expand Down
26 changes: 19 additions & 7 deletions Libraries/Network/RCTHTTPRequestHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ @interface RCTHTTPRequestHandler () <NSURLSessionDataDelegate, RCTTurboModule>

@end

static NSURLSessionConfigurationProvider urlSessionConfigurationProvider;

void RCTSetCustomNSURLSessionConfigurationProvider(NSURLSessionConfigurationProvider provider) {
urlSessionConfigurationProvider = provider;
}

@implementation RCTHTTPRequestHandler
{
NSMapTable *_delegates;
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit c299694

Please sign in to comment.