Norse
Norse

Reputation: 648

How to display an alert when app launches

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?

Upvotes: 0

Views: 2997

Answers (1)

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

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)
    }
}

Upvotes: 2

Related Questions