SagittariusA
SagittariusA

Reputation: 5427

"Application tried to present modally an active controller" in UIImagePicker with UIAlertController

In my app I want the user to keep the iphone as horizzontally flat as possibile while he/she takes a picture, so I thought to show an alert when the tilt is too bad:

@IBAction func takeSnapshot(sender: UIButton) {
    self.imagePicker =  UIImagePickerController()
    self.imagePicker.delegate = self
    self.imagePicker.sourceType = .Camera
    self.startMotionManager()
    presentViewController(imagePicker, animated: true, completion: nil)
}

func startMotionManager() {

    let alertController = UIAlertController(title: "Attenzione!", message: "Mantieni l'iphone il più orizzontale possibile!", preferredStyle: .Alert)

    var showAlert:Bool = false

    self.motionManager.deviceMotionUpdateInterval = 0.01
    self.motionManager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrame.XArbitraryZVertical, toQueue: NSOperationQueue.currentQueue()!, withHandler:{
        deviceManager, error in
        print(deviceManager?.gravity)
        /* se inclino troppo o troppo poco l'iphone */
        if ((deviceManager?.gravity.z >= -0.999) && (deviceManager?.gravity.z <= -0.980)) {
            if ((!alertController.isViewLoaded()) || (alertController.view.window == nil)) {
                self.imagePicker.presentViewController(alertController, animated: true, completion: nil)
            }
        }
        /* se l'iphone è inclinato bene allora nascondo l'alert */
        else {
            if ((alertController.view.window != nil) || (alertController.isViewLoaded())) {
                alertController.dismissViewControllerAnimated(true, completion: nil)
            }
        }
    })
}

Sometimes, either while the user is taking the photo or even at the very beginning, I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <UIImagePickerController: 0x168ec800>.'

I have also tried with something simpler like:

func startMotionManager() {

    let alertController = UIAlertController(title: "Attenzione!", message: "Mantieni l'iphone il più orizzontale possibile!", preferredStyle: .Alert)

    var showAlert:Bool = false

    self.motionManager.deviceMotionUpdateInterval = 0.01
    self.motionManager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrame.XArbitraryZVertical, toQueue: NSOperationQueue.currentQueue()!, withHandler:{
        deviceManager, error in
        print(deviceManager?.gravity)
        /* se inclino troppo o troppo poco l'iphone */
        if ((deviceManager?.gravity.z >= -0.999) && (deviceManager?.gravity.z <= -0.980)) {
            if (!showAlert:Bool) {
                showAlert = true
                self.imagePicker.presentViewController(alertController, animated: true, completion: nil)
            }
        }
        /* se l'iphone è inclinato bene allora nascondo l'alert */
        else {
            if (showAlert) {
                showAlert = false
                alertController.dismissViewControllerAnimated(true, completion: nil)
            }
        }
    })
}

but it doesn't work either. I have read many discussions about this in SO and tried other configurations but they don't work. How can I fix it?

Upvotes: 0

Views: 7092

Answers (1)

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

Try to replace this line:

presentViewController(imagePicker, animated: true, completion: nil)

With this

self.navigationController?.pushViewController(imagePicker, animated: YES)

Upvotes: 4

Related Questions