Swift: "Attempt to present UIAlertController whose view is not in the window hierarchy!" Swift: "Attempt to present UIAlertController whose view is not in the window hierarchy!" swift swift

Swift: "Attempt to present UIAlertController whose view is not in the window hierarchy!"


try to present your UIAlertController in a DispatchQueue

DispatchQueue.main.async {    let alert = UIAlertController(title: "Alert!", message: nil, preferredStyle: .alert)    let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)    alert.addAction(cancelAction)    self.present(alert, animated: true, completion: nil)   }


This line:

ViewController().present(alertView!, animated: true, completion: nil)

creates a new instance of ViewController and calls the present method on it. That won't work. You need to call it from a view controller that is itself presented. It looks like that code is inside ConsoleViewController, maybe you can just use self there.