Custom http header for MapBox IOS SDK with Swift

Viewed 258

How to add custom http header value when making tile request to any wms service with Swift?

I've seen a few examples written in OBJECTIVE-C that are said to work, but I haven't been able to do it with swift.

I tried to set the MGLNetworkConfiguration.sharedManager.sessionConfiguration.httpAdditionalHeaders value, but I couldn't see the http headers I sent in the requests.

1 Answers

When you say that you added your httpAdditionalHeaders, where exactly in your setup do you do this?

From the docs MGLNetworkConfiguration

Assign this object before instantiating any MGLMapView object, or using MGLOfflineStorage.

NOTE

NSURLSession objects store a copy of this configuration. Any further changes to mutable properties on this configuration object passed to a session’s initializer will not affect the behavior of that session.

Here's what you can try -

let sessionConfig = URLSessionConfiguration.default
sessionConfig.httpAdditionalHeaders = [:] // your headers here
MGLNetworkConfiguration.sharedManager.sessionConfiguration = sessionConfig

And make sure this call happens in the app before instantiating any MGLMapView object, or using MGLOfflineStorage.

Related