Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios] Specify custom NSURLSessionDelegate #13491

Closed
wants to merge 6 commits into from

Conversation

halset
Copy link
Contributor

@halset halset commented Dec 1, 2018

This is an attempt to solve #11888 by letting the app provide a custom NSURLSessionDelegate by setting "MGLNSURLSessionDelegateClassName" in Info.plist.

My main motivation for doing this is to be able to use tile services that uses HTTP Basic Authentication.

Copy link
Contributor

@julianrex julianrex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR @halset!

Would you mind adding an entry to the iOS and macOS changelog files?

Noting that longer term we may want to consider using NSUserDefaults (similar to how we've had to add support for MGLCollisionBehaviorPre4_0 in #13426)

@halset
Copy link
Contributor Author

halset commented Dec 9, 2018

@julianrex does the changelogs look good?

@julianrex
Copy link
Contributor

Looks good - thanks @halset!

NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *delegateClassName = [info valueForKey:@"MGLNSURLSessionDelegateClassName"];
if (delegateClassName) {
sessionDelegate = [[NSClassFromString(delegateClassName) alloc] init];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Would you mind documenting MGLNSURLSessionDelegateClassName in the following two documents?

https://github.com/mapbox/mapbox-gl-native/blob/master/platform/ios/docs/guides/Info.plist%20Keys.md
https://github.com/mapbox/mapbox-gl-native/blob/master/platform/macos/docs/guides/Info.plist%20Keys.md

Some points that could be covered, since I think this is the first time we’ve introduced an API that represents a class name as a string:

  • If the class is implemented in Swift, it needs to bridge to Objective-C.
  • The key needs to be set to a runtime class name. (A Swift application would either need to give the class an Objective-C name or set the key to a mangled name.)
  • The class should conform to the NSURLSessionDelegate protocol. (Since the protocol’s methods are all optional, we can’t check for conformance at runtime. If the class doesn’t conform to the protocol, there wouldn’t be any adverse effects, but that would probably be a mistake on the part of the developer.)
  • See also MGLOfflineStorageDelegate. (Like the new Info.plist key, that protocol is used for any request initiated by mbgl but presumably not for Mapbox Telemetry requests, which are handled by a different dependency.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halset are you planning on updating this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julianrex I still want this to be merged, but the documentation work suggested by @1ec5 has not bubbled up to the top of my priority list yet. Sorry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! Was just checking in 🙂

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could add a method mgl_applicationInfoDictionary to NSBundle+MGLAdditions.m and use that here instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@datwelk good idea. I've added your comment as a new issue: #13908 - so that we can keep that as a separate PR.

@julianrex julianrex added iOS Mapbox Maps SDK for iOS ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold labels Feb 11, 2019
@julianrex
Copy link
Contributor

julianrex commented Feb 11, 2019

@halset when you're ready for this to be reviewed again, please remove the DO NOT MERGE label.

@halset halset requested a review from a team February 18, 2019 14:02
@halset
Copy link
Contributor Author

halset commented Feb 18, 2019

@julianrex Sorry for the delay. I have finally added the Info.plist documentation, but I do not know if I have privileges to remove the DO NOT MERGE label.

@halset
Copy link
Contributor Author

halset commented Feb 18, 2019

I have updated this with the latest from #13886. Is that okay or should this be moved from a Info.plist key to something in MGLNetworkConfiguration?

@lucianboboc
Copy link

NSURLSessionDelegate is limited, if the backend will require an Authorization header with a Bearer xxx value there is no way to add that using NSURLSessionDelegate. It only supports Basic authentication (username + password, more details here: https://blog.cocoafrog.de/2017/11/18/how-nsurlsession-authentication-should-work.html).
A hook method before the request is sent is what's needed for the URLRequest to be configured.

@julianrex julianrex removed the ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold label Feb 20, 2019
@julianrex
Copy link
Contributor

I have updated this with the latest from #13886. Is that okay or should this be moved from a Info.plist key to something in MGLNetworkConfiguration?

@fabian-guerra would you mind looking at this PR please, especially in context of your recent changes to MGLNetworkConfiguration? I'm wondering (like @halset) if this is the right time to add this to the configuration object.

@lucianboboc thanks for the link - that's very clear. We realize that this PR is limited in scope, however authentication is something that is on our radar. /cc @tmpsantos

Copy link
Contributor

@fabian-guerra fabian-guerra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making this change. I'm ok with this current implementation. But now that we have made some changes to expose the network configuration object it may be worth it to consider a different approach.

@@ -69,6 +69,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
* Points of interest have clearer, localized VoiceOver hints in styles that use version 8 of the Mapbox Streets source. ([#13525](https://github.com/mapbox/mapbox-gl-native/pull/13525))
* Added `MGLLoggingConfiguration` and `MGLLoggingBlockHandler` that handle error and fault events produced by the SDK. ([#13235](https://github.com/mapbox/mapbox-gl-native/pull/13235))
* Fixed random crashes during app termination. ([#13367](https://github.com/mapbox/mapbox-gl-native/pull/13367))
* Specify custom NSURLSessionDelegate with Info.plist key MGLNSURLSessionDelegateClassName. ([#13491](https://github.com/mapbox/mapbox-gl-native/pull/13491))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using verbs at the beginning of the changelog entry use the past tense.
Enclose any class/method/keyword with ``.
This entry should be in the latest version: 4.9.0

@@ -40,6 +40,7 @@
* Renamed `-[MGLOfflineStorage putResourceWithUrl:data:modified:expires:etag:mustRevalidate:]` to `-[MGLOfflineStorage preloadData:forURL:modificationDate:expirationDate:eTag:mustRevalidate:]`. ([#13318](https://github.com/mapbox/mapbox-gl-native/pull/13318))
* `MGLMapSnapshotter` now respects the `MGLIdeographicFontFamilyName` key in Info.plist, which reduces bandwidth consumption when snapshotting regions that contain Chinese or Japanese characters. ([#13427](https://github.com/mapbox/mapbox-gl-native/pull/13427))
* Added `MGLLoggingConfiguration` and `MGLLoggingBlockHandler` that handle error and fault events produced by the SDK. ([#13235](https://github.com/mapbox/mapbox-gl-native/pull/13235))
* Specify custom NSURLSessionDelegate with Info.plist key MGLNSURLSessionDelegateClassName. ([#13491](https://github.com/mapbox/mapbox-gl-native/pull/13491))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

// app might provide a NSURLSessionDelegate for authentication and such
id<NSURLSessionDelegate> sessionDelegate = nil;
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *delegateClassName = [info valueForKey:@"MGLNSURLSessionDelegateClassName"];
Copy link
Contributor

@fabian-guerra fabian-guerra Feb 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that MGLNetworkConfiguration is public what do you think if instead of reading a class from the Info file we register the class in the network configuration singleton?

  • Reading from the plist file is a bit hidden.
  • This way it's clear where to look if anyone requires custom authentication.
  • It's safer instantiating a class than parsing first a string.

@stale stale bot added the archived Archived because of inactivity label Apr 22, 2019
@stale
Copy link

stale bot commented Apr 23, 2019

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

@stale stale bot closed this Apr 23, 2019
@julianrex julianrex reopened this Apr 23, 2019
@stale stale bot removed the archived Archived because of inactivity label Apr 23, 2019
@julianrex
Copy link
Contributor

Re-opening: @halset do you have any feedback on @fabian-guerra's comment above about using MGLNetworkConfiguration instead?

@halset
Copy link
Contributor Author

halset commented May 21, 2019

@julianrex Looking at @fabian-guerra's comment, this pull-request has probably been out-dated by the MGLNetworkConfiguration. We should probably just close this pull-request and then hopefully someone will start a new one based on MGLNetworkConfiguration. I am continuing to use this patch in my app as it is in this pull-request.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
iOS Mapbox Maps SDK for iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants