Application tried to present modally an active controller : UIImagePickerController

Tar_Tw45 picture Tar_Tw45 · Mar 1, 2013 · Viewed 16.7k times · Source

I'm struggle at this for 2 days and believe that this is the moment I should call for help. After I search SOF for a while, none of any answer could solve my problem. Here are my application ...

In the application,

  • Device is iPad, iOS 6
  • RootViewController is NavigationController
  • TopViewController is TabBarController
  • In this TabBarController, I present a popoverController from right bar button of navigation bar
  • In presenting popover there is a button to allow user to pick image from by taking new one or pick from existing.
  • To pick new one, I presentViewController UIImagePickerController to allow user to take photo with divice camera. presentModalViewController:animated: if iOS < 6, and presentViewController:animated:completion: for iOS > 6
  • I also hide Status Bar before presentation
  • To select from existing photo, I do presentPopoverFromBarButtonItem:permitArrowDirections:animated:
  • PopoverViewController also referencing by A TabBarController

Here is the issue

  • Present UIImagePickerController will always failed if user try to pick new one first with exception "Application tried to present modally an active controller <[name of view controller that try to present]>"
  • BUT, if user try to pick image from camera roll for once and then try to take new one again, it won't fail.

Here are what I tried

  • present from RootViewController
  • present from TopViewController (TabBarController)
  • present from popoverViewController itself
  • present from a tab of TabBarController
  • hide popoverViewController before presentation
  • resignFirstResponder from a textField in popoverViewController

Here is the current code I'm using

// PopoverViewController, presented by a tab in TabBarController
- (IBAction)takePhoto:(id)sender {
    [self.delegate takePhotoWithDeviceCamera];
}

// A Tab in TabBarController, delegate of popoverViewController
- (void)takePhotoWithCamera {
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    if ([UIDevice OSVersion] < 6.0) {
        [self presentModalViewController:cameraPicker animated:YES];
    } else {
        [self presentViewController:cameraPicker animated:YES completion:nil];
    }
}

Any idea what would cause this error? Any suggestion are welcome. Thank you.

Answer

JordiVilaplana picture JordiVilaplana · Jul 31, 2015

Got the same trouble than you and finally got the solution based on @CainaSouza's answer. I've been working with Xamarin.iOS so I'll make my answer in C#, but it can be easily translated to Objective-C.

I'm using the same code as @CainaSouza to call the controller:

UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController (customController, true, null);

And then I add the following code to my custom RootViewController:

public override void PresentViewController (UIViewController viewControllerToPresent, bool animated, Action completionHandler)
{
    if (PresentedViewController != viewControllerToPresent) {
        base.PresentViewController (viewControllerToPresent, animated, completionHandler);
    }
}

The trick is to check if you haven't presented that UIViewController before.

I know it's an old question, but hope it will help someone. :)