Mapbox-gl-native: ios: Support custom NSURLProtocol

Created on 29 May 2018  路  14Comments  路  Source: mapbox/mapbox-gl-native

Platform: iOS
Mapbox SDK version: 4.0

Based on the discussion in #3597.

To modify the HTTP request headers or, in general, handle all sorts of remote requests, it's desirable to be able to use custom NSURLProtocol implementations. For this to work, we'd need a way to add NSURLProtocol classes to NSURLSessionConfiguration.protocolClasses (as described here).

How would we go about this? A delegate call? A class property?

I'm unfamiliar with the codebase but this is as I understand it so far:

  1. The NSURLSession is created in HTTPFileSource::Impl() in darwin/src/http_file_source.mm
  2. This is instantiated by DefaultFileSource::Impl() in platform/default/default_file_source.cpp
  3. which is called by SDKOfflineStorage-init in SDK/Foundation/Offline Maps/SDKOfflineStorage.mm (real path is darwin/src/MGLOfflineStorage.mm)
  4. This initialization happes due to [MGLOfflineStorage sharedOfflineStorage].mbglFileSource being accessed in
  5. MGLRendererConfiguration.fileSource in SDK/Foundation/MGLRendererConfiguration.mm (also darwin/src/)
  6. accessed in MGLMapView-commonInit in SDK/Kit/MGLMapView.mm (ios/src/)

So, MapView -> Renderer -> Offline Storage -> Default File Source -> HTTP File Source.

SDKOfflineStorage and MGLMapView do have delegates, but we can't use either because HTTPFileSource::Impl() is called during their initialization. We could add an additional parameter to an MGLMapView-init variation. However that would require to pass it through platform/default/default_source_file.cpp, which is undesirable, I guess. The only option I see would be a class property on...some class. HTTPFileSource doesn't have a ObjC pendant yet. Could we add one? Or have it access some other global state thing?

The Android solution is implemented in #10948. Would the equivalent be to have SDK/[Foundation|Kit]/MGLHTTPRequestUtil.{h,mm} and access that in HTTPFileSource::Impl()?

Edit: Is it even possible to access anything in the SDK part from the platform part?

I'm pretty much lost here. Any thoughts, @1ec5? Could you point me in a direction?

Also, are fonts/glyphs downloaded via the same mechanism?

feature iOS

Most helpful comment

Stay open, plz.

All 14 comments

Having thought about it a little bit more, I think it'd be best if it'd be possible to provide the whole NSURLSessionConfiguration.

Still no clue how to go about this, though oO ARCHITECTURE.md affirms my understanding that the SDK uses and accesses the C++ core, not the other way around. Could we have a NSURLSessionConfiguration setter on...MGLOfflineStorage that somehow accesses the used HTTPFileSource::Impl and passes the given value through? HTTPFileSource::Impl would then need to re-initialize its session.

Would be really nice to have this.

Is it even possible to access anything in the SDK part from the platform part?

Not from the SDK鈥檚 public API, because the SDK is intended to be compatible with pure Objective-C projects, whereas mbgl鈥檚 public API is entirely in C++. It is possible for the SDK鈥檚 internal code to interact with mbgl鈥檚 public API, but the networking code is internal even to mbgl.

the SDK uses and accesses the C++ core, not the other way around

This is true for the most part. mbgl does have some platform-specific Objective-C++ code at the very lowest level for working with NSURLSession.

@kkaefer, do you think we could expose a way for developers to customize NSURLSessionConfiguration? I think this would be one important step towards resolving #4291 as well.

/cc @julianrex

I think it's also worth looking at what's happening over in the Android version re customizing networking.

This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

This might be a way to go forward: https://github.com/mapbox/mapbox-gl-native/pull/13491

This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

Stay open, plz.

This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

@fabian-guerra is there a label we can add so the stale bot ignores it? (and remove the "archived" label)

Edit: No, there currently isn't. So the project would need a new label and a stale.yml PR first.

Still, stalebot seems to be malfunctioning 鈥斅爑nsure why it鈥檚 trying to continually close issues after mere hours of inactivity. /cc @tmpsantos

I'm going to assign this to me for now.

Still, stalebot seems to be malfunctioning 鈥斅爑nsure why it鈥檚 trying to continually close issues after mere hours of inactivity. /cc @tmpsantos

It is because the label archived was not removed. The bot will mark as archived after detecting the inactivity and then close.

For those of you who land on this page and looking for similar capability on the Android side, read this ticket:

https://github.com/mapbox/mapbox-gl-native/issues/13835

Was this page helpful?
0 / 5 - 0 ratings