mapbox-maps-ios
mapbox-maps-ios copied to clipboard
Attribution crashes because parentViewController is nil
Environment
- Xcode version: 14
- iOS version: latest
- Devices affected: all
- Maps SDK Version: 10.10.1
Observed behavior and steps to reproduce
The attribution buttons tries to open the dialog on the parentViewController here: https://github.com/mapbox/mapbox-maps-ios/blob/192437b31f46860d10900e227dfc36ef30aaa130/Sources/MapboxMaps/Foundation/MapView%2BAttribution.swift#L6
However, this can be nil (SwiftUI, Ionic/cordova etc.) and leads to a crash.
Expected behavior
To not crash 😇
Additional links and references
A quick workaround for me is:
func viewControllerForPresenting(_ attributionDialogManager: AttributionDialogManager) -> UIViewController {
return (UIApplication.shared.delegate?.window??.rootViewController!)!
}
This could serve as a fallback in case the parentViewController is actually nil.
/cc @persidskiy
Hi @proggeramlug, thank you for highlighting this. I've created an internal issue to fix it.
Can you please specify in which cases you observe parentViewController to be nil. I've tried to put MapView into SwiftUI-only app on iOS 15-16 , but don't observe such crash, the parentViewController is UIHostingController in that case.
@persidskiy Thanks!
In our case we are rendering a native extension on top of an Ionic/Cordova app. So the review is created and put on top of a WKWebview.
We do this by adding the newly created MapView (created via frame) to the stack where the WKWebview is, like this:
self.webView?.superview?.addSubview(self.mapView!)
@proggeramlug Can I ask you to create a minimal sample where we can debug and reproduce it?
This parentViewController should find a UIViewController eligible for presenting the attribution as long as MapView is presented inside a ViewController.
https://github.com/mapbox/mapbox-maps-ios/blob/main/Sources/MapboxMaps/Foundation/Extensions/UIView.swift#L2-L11
Your case seem to be the edge case parentViewController doesn't cover, it would be nice to fix it.