question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ios] Adding URLProtocol to MGLNetworkConfiguration does not work

See original GitHub issue

Hi, 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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Mr-Alirezaacommented, Aug 28, 2020

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 custom URLProtocol classes with higher priorities at the launch of the application. So my custom URLProtocol 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”.

1reaction
mfazekascommented, Apr 9, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found