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

[ios] Attribution button in MGLMapView doesn't present alert controller from modal view controller #8782

Closed
cmoulton opened this issue Apr 20, 2017 · 5 comments
Assignees
Labels
bug iOS Mapbox Maps SDK for iOS release blocker Blocks the next final release
Milestone

Comments

@cmoulton
Copy link

Platform:

iOS

Mapbox SDK version:

3.5.2 via cocoapods

Steps to trigger behavior

  1. Add modally presented VC
  2. Add map view to modally presented VC
  3. Run app, navigate to map view
  4. Tap attribution button

Expected behavior

Attribution UIAlertController displayed

Actual behavior

No change in UI (other than button highlight state). Error shown in console:

Warning: Attempt to present <UIAlertController: 0x7fc73e62f8c0> on 
<MapboxModalAttribution.ViewController: 0x7fc73e40cb00> whose view is not in the window hierarchy!

Demo Project

https://github.com/cmoulton/MapboxModalAttributionTest

Need to set mapbox access token in info.plist to use.

screen shot 2017-04-20 at 10 21 14 am

(Map views in green)

I think the change required is to change this code in MGLMapView.mm (line 1947):

UIViewController *viewController = self.window.rootViewController;
if ([viewController isKindOfClass:[UINavigationController class]]) {
    viewController = [(UINavigationController *)viewController viewControllers].firstObject;
}
[viewController presentViewController:attributionController
                             animated:YES
                           completion:NULL];

To fetch the current top view controller (based on http://stackoverflow.com/a/21848247/90743):

UIViewController *viewController = [UIViewController getTopViewControllerFrom:[self.window.rootViewController topViewController]];
[viewController presentViewController:attributionController
                             animated:YES
                           completion:NULL];

// in UIViewController extension
+ (UIViewController *)getTopViewControllerFrom:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [UIViewController getTopViewControllerFrom:[((UINavigationController *)vc) visibleViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [UIViewController getTopViewControllerFrom:[((UITabBarController *)vc) selectedViewController]];
    } else if (vc.presentedViewController) {
            return [UIViewController getTopViewControllerFrom:vc.presentedViewController];
    }
    return vc;
}
@boundsj boundsj added iOS Mapbox Maps SDK for iOS bug labels Apr 20, 2017
@ab-rdarts
Copy link

ab-rdarts commented Apr 20, 2017

I have same problem when pressing "i" button on the map which is on modal view controller.

Warning: Attempt to present <UIAlertController: 0x7fc8e20378f0> on <OurViewController: 0x7fc8e20107e0> whose view is not in the window hierarchy!

@1ec5
Copy link
Contributor

1ec5 commented Apr 25, 2017

This regression was introduced by #8371. Thank you for the suggested fix, @cmoulton!

/cc @friedbunny

@1ec5 1ec5 added this to the ios-v3.6.0 milestone Apr 25, 2017
@1ec5 1ec5 self-assigned this Apr 26, 2017
@1ec5 1ec5 added the release blocker Blocks the next final release label Apr 26, 2017
@boundsj boundsj modified the milestones: ios-v3.5.3, ios-v3.6.0 Apr 26, 2017
@incanus
Copy link
Contributor

incanus commented Apr 27, 2017

Thanks for the excellent demo project and fix @cmoulton!

@jmkiley jmkiley closed this as completed in e5ae80c May 2, 2017
@kmorey
Copy link

kmorey commented May 11, 2017

This fixed the issue with the UIAlertController not opening from the attribution button, but the nested telemetry UIAlertController won't open for what appears to be the same issue. I can open a separate issue, but it seems to possibly be the same issue as this one just in a different area.

@boundsj
Copy link
Contributor

boundsj commented May 11, 2017

Thanks @kmorey. Can you please create a new issue for the telemetry controller specifically?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug iOS Mapbox Maps SDK for iOS release blocker Blocks the next final release
Projects
None yet
Development

No branches or pull requests

6 participants