How to display an alert when app launches

岁酱吖の 提交于 2019-12-12 03:33:35

问题


I want to present an alert when my app launches for the first time that day. I think the place to do this is the appDelegate (correct me if i am wrong). I have two problems, one: i don't know which of the functions in the appDelegate the function should reside under (have currently chosen just func application), and second: i don't know how to present the alertController to the view that is open. Thus far i have done this

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if checkIfNewDay() {
        let alert = UIAlertController("some title": alertTitle, message: "some message", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Okay", style: .Cancel, handler: nil))

        // What to do here????
    }

What code should i replace the comment with?


回答1:


Try to use this methode : applicationDidBecomeActive

func applicationDidBecomeActive(application: UIApplication) {
     //This method is called when the rootViewController is set and the view.
    if checkIfNewDay() {
       let alert = UIAlertController("some title": alertTitle, message: "some message", preferredStyle: .Alert)
       alert.addAction(UIAlertAction(title: "Okay", style: .Cancel, handler: nil))
       self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
    }
}


来源:https://stackoverflow.com/questions/35672879/how-to-display-an-alert-when-app-launches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!