[ios] Adding URLProtocol to MGLNetworkConfiguration does not work
See original GitHub issueHi,
I need to intercept all network calls made by Mapbox’s SDK. I add a couple of lines of code to the iOS source code to add my own URLProtocol
subclass to a URLSessionConfiguration
and finally assign the URLSessionConfiguration
to the MGLNetworkConfiguration.sharedConfiguration.sessionConfiguration
.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSMutableArray *classes = [configuration.protocolClasses mutableCopy];
[classes insertObject:[CustomURLProtocol class] atIndex:0];
configuration.protocolClasses = classes;
[MGLNetworkConfiguration.sharedManager setSessionConfiguration:configuration];
This is needed to be done before the initialization of any MGLMapView instance.
I add these lines in RCTMGLMapView.m
in - (instancetype)initWithFrame:(CGRect)frame
initializer like below:
- (instancetype)initWithFrame:(CGRect)frame
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSMutableArray *classes = [configuration.protocolClasses mutableCopy];
[classes insertObject:[CustomURLProtocol class] atIndex:0];
configuration.protocolClasses = classes;
[MGLNetworkConfiguration.sharedManager setSessionConfiguration:configuration];
if (self = [super initWithFrame:frame]) {
_pendingInitialLayout = YES;
...
}
return self;
}
This technique does not work in React Native. But the same thing works in a pure iOS project with Objective-C or Swift.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSMutableArray *classes = [configuration.protocolClasses mutableCopy];
[classes insertObject:[CustomURLProtocol class] atIndex:0];
configuration.protocolClasses = classes;
[MGLNetworkConfiguration.sharedManager setSessionConfiguration:configuration];
MGLMapView *mapView = [[MGLMapView alloc] initWithFrame: ...];
Any idea about why this does not work in React Native?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
[ios] Adding URLProtocol to MGLNetworkConfiguration does ...
This technique does not work in React Native. But the same thing works in a pure iOS project with Objective-C or Swift.
Read more >Mapbox 10 `MGLNetworkConfiguration` replacement
I am migrating from Mapbox 6 to Mapbox 10. I have a source, which needs a authorisation header in order to be accessed....
Read more >URLProtocol timeout for post/put/p… | Apple Developer Forums
It is working as expected for a GET request. Any idea why it is failing for requests sending data to the server? I've...
Read more >MGLNetworkConfiguration | Maplibre gl native ios | MapTiler
The session configuration object that is used by the NSURLSession objects in this SDK. If this property is set to nil or if...
Read more >How to stub network in iOS - Thuyen's corner
URLProtocol.registerClass(_:) does not help now… A solution could be adopted by adding our custom class to configuration.protocolClasses .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @snazarkoo, I couldn’t use my own subclass of
URLProtocol
to solve my problem. Since my company is using Mapbox SDK to show its tiles and style I needed it to send some custom request headers with all (especially style and tiles) requests. This solution was only usable when I wasn’t using react-native SDK. I think this is because of that react-native SDK itself is registering some customURLProtocol
classes with higher priorities at the launch of the application. So my customURLProtocol
will not be used. I am an iOS engineer and I don’t have any specialty in React-Native but I think there is a way provided by Mapbox React-Native SDK to add custom headers to the requests. I think it might be helpful to google terms like “mapbox react native sdk change tile headers” or “custom headers in mapbox react native sdk”.@Mr-Alirezaa i’m closing this, this is very special usecase. You can try this in a bar bone RN app, to see if it’s issue in our plugin or in RN env.