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?