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: https://github.com/react-native-community/discussions-and-proposals/issues/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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Håkon Knutzen 2021-06-02 18:38:52 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 0d32aef3aa
Коммит 58444c74f5
2 изменённых файлов: 24 добавлений и 7 удалений

Просмотреть файл

@ -8,6 +8,11 @@
#import <React/RCTInvalidating.h> #import <React/RCTInvalidating.h>
#import <React/RCTURLRequestHandler.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. * This is the default RCTURLRequestHandler implementation for HTTP requests.
*/ */

Просмотреть файл

@ -18,6 +18,12 @@
@end @end
static NSURLSessionConfigurationProvider urlSessionConfigurationProvider;
void RCTSetCustomNSURLSessionConfigurationProvider(NSURLSessionConfigurationProvider provider) {
urlSessionConfigurationProvider = provider;
}
@implementation RCTHTTPRequestHandler @implementation RCTHTTPRequestHandler
{ {
NSMapTable *_delegates; NSMapTable *_delegates;
@ -75,14 +81,20 @@ RCT_EXPORT_MODULE()
NSOperationQueue *callbackQueue = [NSOperationQueue new]; NSOperationQueue *callbackQueue = [NSOperationQueue new];
callbackQueue.maxConcurrentOperationCount = 1; callbackQueue.maxConcurrentOperationCount = 1;
callbackQueue.underlyingQueue = [[_moduleRegistry moduleForName:"Networking"] methodQueue]; callbackQueue.underlyingQueue = [[_moduleRegistry moduleForName:"Networking"] methodQueue];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSessionConfiguration *configuration;
// Set allowsCellularAccess to NO ONLY if key ReactNetworkForceWifiOnly exists AND its value is YES if (urlSessionConfigurationProvider) {
if (useWifiOnly) { configuration = urlSessionConfigurationProvider();
configuration.allowsCellularAccess = ![useWifiOnly boolValue]; } 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]; assert(configuration != nil);
[configuration setHTTPCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
[configuration setHTTPCookieStorage:[NSHTTPCookieStorage sharedHTTPCookieStorage]];
_session = [NSURLSession sessionWithConfiguration:configuration _session = [NSURLSession sessionWithConfiguration:configuration
delegate:self delegate:self
delegateQueue:callbackQueue]; delegateQueue:callbackQueue];