转载自：《How To Present An Alert View Using UIAlertController When You Don't Have A View Controller》Over the summer I updated a number of apps to iOS 9. A common tasks for me was replacing the deprecated UIAlertView with new UIAlertController class. Generally this is easy to do, but there is one scenario that UIAlertView handles well that UIAlertController doesn’t support at all: displaying an alert view from parts of your code without a reference to the current view controller, for instance, displaying an alert view from the app delegate.
Trying to display an alert view using UIAlertController from a non-UI specific class such as the app delegate is a common problem, and developers have found different workarounds. One particular solution that caught my eye was this one from agilityvision, who writes:At WWDC I stopped in at one of the labs and asked an Apple Engineer this same question: “What was the best practice for displaying a UIAlertController?” And he said they had been getting this question a lot and we joked that they should have had a session on it. He said that internally Apple is creating a UIWindow with a transparent UIViewController and then presenting