iOS 在应用程序委托中弹出消息?

TFL*_*9Z1 1 ios appdelegate swift uialertcontroller

我尝试在我的应用程序委托中创建弹出警报消息,但它根本没有显示。程序是用Swift 4编写的。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

tru*_*duc 5

根据文档didFinishLaunching

告诉委托人启动过程即将完成并且应用程序即将准备好运行。

所以我认为你不能在 中显示警报didFinishLaunchingWithOptions

尝试将您的代码移至applicationDidBecomeActive.