0

I have a local alert set to fire off at a predesignated time (30 seconds). What I want to do is fire off an alert at 20 seconds. This is my relevant appDelegate code:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    // Pass the "firing" event onto the notification manager
    timerNotificationManager.timerFired()
    if application.applicationState == .Active {
        //let alert = UIAlertController(title: "NotifyTimely", message: "Your time is up", preferredStyle: .Alert)
        // Handler for each of the actions
        let actionAndDismiss = {
            (action: String?) -> ((UIAlertAction!) -> ()) in
            return {
                _ in
                self.timerNotificationManager.handleActionWithIdentifier(action)
                self.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)
            }
        }
        /*
        alert.addAction(UIAlertAction(title: "Dismiss", style: .Cancel, handler: actionAndDismiss(nil)))
        alert.addAction(UIAlertAction(title: "Restart", style: .Default, handler: actionAndDismiss(restartTimerActionString)))
        alert.addAction(UIAlertAction(title: "Snooze", style: .Destructive, handler: actionAndDismiss(snoozeTimerActionString)))
        window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
        */

        var ourAlert = UIAlertView(title: "Time Alert", message: "You have been active for 20 seconds!", delegate: nil, cancelButtonTitle: "Dismiss")
        ourAlert.show()
        self.finalAlert()
    }
}

func finalAlert() {
    let alert = UIAlertView()
    alert.title = "Final Timer Alert"
    alert.message = "You have been active for 20 seconds. Your ride is now being charged."
    alert.addButtonWithTitle("OK")
    alert.show()
}

Now I have seen this answer How can I use NSTimer in Swift?

But I don't want the finalAlert function to kick off immediately. I want it to fire off 10s after the initial alert. How do I get NSTimer to wait 10 seconds to fire the alert or is there a better way to wait?

Community
  • 1
  • 1
lostinthebits
  • 661
  • 2
  • 10
  • 23
  • possible duplicate of [Swift performSelector: withObject: afterDelay:](http://stackoverflow.com/questions/24170282/swift-performselector-withobject-afterdelay) – StilesCrisis Feb 18 '15 at 04:03

2 Answers2

1

wait 10 seconds

It's unclear to me what you think is wrong with the behavior of NSTimer, but in any case the simplest way to express the notion "do this, 10 seconds from now" is to use GCD's dispatch_after. And the easiest way to do that is as I encapsulate it here: dispatch_after - GCD in swift?

Community
  • 1
  • 1
matt
  • 447,615
  • 74
  • 748
  • 977
1

NSTimer is literally meant to not fire immediately. You start the timer with a time interval of when you want it to fire, I guess in your case 10 seconds.

//first parameter is a time interval in seconds target is self and selector is
//finalAlert, which means after 10 seconds it will call self.finalAlert
//userInfo is nil, because you aren't passing any additional info
//and repeats is false, because you only want the timer to run once.
let timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "finalAlert", userInfo: nil, repeats: false)
Jeremy Pope
  • 3,224
  • 1
  • 14
  • 17