maplibre-native icon indicating copy to clipboard operation
maplibre-native copied to clipboard

iOS networking overrides

Open ManWithBear opened this issue 3 years ago • 3 comments

Greetings,

Do you have any plans on updating networking API in iOS SDK? Especially way to add additional headers / modify url for requests?

Right now in Android SDK you can provide a custom network routing via:

com.mapbox.mapboxsdk.module.http.HttpRequestUtil.setOkHttpClient(...)

In Web SDK you can provide a transformRequest lambda to modify outgoing requests.

On both platforms overrides are per map instance.

On iOS only option is to provide a custom URLProtocol and it's SDK wide, which is unreasonably complicated and inflexible.

Use case: Our app provide multiple authentication options, such as: subscriptionKey query parameter, Authorization header, etc. Different map instances may use different options.
Multiple maps may exist at the same time.

Currently we required to:

  1. have global reference on all map instances
  2. determine from which map request comes based on URLRequest / URL string

Having a way to modify requests before SDK would send them on per map basis would simplify a lot of things for us.

ManWithBear avatar Jul 14 '21 21:07 ManWithBear

I think you could use MGLOfflineStorageDelegate to modify URLs in your requests. For example, Amazon is using it to sign URLs.

petr-pokorny-1 avatar Jul 16 '21 08:07 petr-pokorny-1

Amazon Location does make use of the Offline storage delegate to perform its signing. See the iOS project aws-samples/amazon-location-samples:MapView.swift#L21,L25. Amazon Location specific signing is implemented in AWSSignatureV4Delegate, and is using MapLibre.

Thanks @petr-pokorny-1 for pointing out this usage of the Offline storage delegate.

roblabs avatar Jul 16 '21 15:07 roblabs

Thanks, I would give it a try, but using offline delegate for online maps sounds counterintuitive. :D

If I understood it right, you need to:

  1. create class that conforms to MGLOfflineStorageDelegate
  2. pass this instance into MGLOfflineStorage.delegate

This solution solves most of issues with current URLProtocol approach, except one: MGLOfflineStorage is a singleton and this delegate is SDK wide.

ManWithBear avatar Jul 19 '21 17:07 ManWithBear