The issue in most questions similar to this one is that present
is called before viewDidAppear
. That is not the reason here.
This app does not use Storyboards of NIBs, and all operations are programmatic.
The rootViewController
of the window of the app is a UISplitViewController
. The view controllers of the split view are set to an array of two UINavigationController
s. A child view controller then modally presents a view controller. The problem is that UIAlertController
s presented from the app delegate won't show while a view controller is being modally presented. It works otherwise.
How I try to present:
window?.rootViewController?.present(alert, animated: true, completion: nil)
I get this error:
Attempt to present UIAlertController on UISplitViewController whose view is not in the window hierarchy
To solve this, I put this function in my app delegate.
// Utility function to avoid:
// Warning: Attempt to present * on * whose view is not in the window hierarchy!
func showAlertGlobally(_ alert: UIAlertController) {
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.windowLevel = UIWindowLevelAlert
alertWindow.rootViewController = UIViewController()
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alert, animated: true, completion: nil)
}