Skip to content

Latest commit

 

History

History
87 lines (62 loc) · 2.33 KB

CustomHttpHeaders.md

File metadata and controls

87 lines (62 loc) · 2.33 KB

Custom http headers

Intro

Custom headers are implemented using OkHttp interseptor for android and method swizzling for iOS.

Method swizzling is done on the [NSMutableURLRequest requestWithURL:] to allow adding headers during runtime.

Prerequisites

Android

None

IOS

To enable this on iOS you need to call [[MLNCustomHeaders sharedInstance] initHeaders] pretty early in the lifecycle of the application. This will swizzle the custom method. Suggested location is [AppDelegate application: didFinishLaunchingWithOptions:]

Working example (AppDelegate.m)

// (1) Include the header file
#import "MLNCustomHeaders.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"SampleApp"
                                            initialProperties:nil];
  // (2) Init headers, add swizzle method
  [[MLNCustomHeaders sharedInstance] initHeaders];
  // (3*) Optionally you can add some global headers here
  [[MLNCustomHeaders sharedInstance] addHeader:@"IP" forHeaderName:@"X-For-Real"];

  ...
  return YES;
}

...

@end

Sending custom http headers with the tile requests

You can configure sending of custom http headers to your tile server. This is to support custom authentication or custom metadata which can't be included in the url.

You can add and remove headers at runtime.

To add a header

    MapLibreGL.addCustomHeader('Authorization', '{auth header}');

To remove a header

    MapLibreGL.removeCustomHeader('Authorization');

Working example

export default class HelloWorldApp extends Component {
  componentDidMount () {
    MapLibreGL.addCustomHeader('Authorization', '{auth header}');
  }

  render () {
    MapLibreGL.addCustomHeader('X-Some-Header', 'my-value');
    return (
      <View style={styles.page}>
        <View style={styles.container}>
          <MapLibreGL.MapView
            style={styles.map}
            styleURL={STYLE_URL}/>
        </View>
      </View>
    );
  }
}