0

This alert controller is initialized:

let alertVC2 = PMAlertController(title: "Need to always enable location authorization", description: "Go to Settings -> App -> Location. Then select 'Always'.", image: UIImage(named: "Location"), style: .alert)

Inside of ViewDidLoad(), action is added to alertVC2.

alertVC2.addAction(PMAlertAction(title: "OK", style: .default, action: { () in

            print("Capture action OK")
            self.alertVC2.dismiss(animated: true, completion: nil)
        }))

 alertVC2.addTextField { (textField) in
            textField?.placeholder = "Location..."
        }

Also, inside the viewDidLoad(), it is added with this snippet of code, which will allow me to run function called willResignActive, when app becomes active again, after staying dormant in the background:

 NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.didBecomeActiveNotification, object: nil)

This is a function that runs when app becomes active again:

@objc func willResignActive(_ notification: Notification) {
        print("activated")

      check()
    }

Inside the check() function, the alertVC2 (AlertController) will be called and the alert controller will be shown on the screen. When I bring the app back to life by bringing it back to action, alertcontroller is displayed. When I exit the app for the second time and return again, however, it will not display alertVC2. When I do this for the third time, app crashes.

Here is brief look at check() function:

func check() {

    if CLLocationManager.authorizationStatus() == .notDetermined || CLLocationManager.authorizationStatus() == .authorizedWhenInUse || CLLocationManager.authorizationStatus() == .denied || CLLocationManager.authorizationStatus() == .restricted {

    locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()

        if CLLocationManager.authorizationStatus() != .authorizedAlways {

        print("Need to always authorize location for me")

        self.present(alertVC2, animated: true, completion: nil)

        }
    }

    if CLLocationManager.authorizationStatus() == .authorizedAlways {

        locationManager.startUpdatingLocation()

    }


}

This is an error message that I receive when app crashed in third attempt:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller

What do I have to do keep it from crashing and continue to post alertcontroller?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
jamryu
  • 668
  • 10
  • 24

1 Answers1

0

It sounds like you're trying to present the alert controller multiple times. If you put a breakpoint on the line where you call self.present, how many times does it hit it?

The easy way out here if what I'm guessing is right, is to check if the view is currently presenting anything, and if it is, skip presenting your alert. Maybe something like:

if CLLocationManager.authorizationStatus() != .authorizedAlways {
    print("Need to always authorize location for me")
    if self.presentedViewController == nil {
        self.present(alertVC2, animated: true, completion: nil)
    }

}

Or maybe something like:

if CLLocationManager.authorizationStatus() != .authorizedAlways {
    print("Need to always authorize location for me")
    if !alertVC2.isBeingPresented { // make sure we're not already presenting this alert to the user
        self.present(alertVC2, animated: true, completion: nil)
    }

}
Chris Hansen
  • 103
  • 4