1

I want to see that my program knows when the timer is done. I'm using WKInterfaceTimer in Xcode 7 Beta 3 (7A152u). The "Tick Tock" prints to the console while the counter is counting down. But when it reaches 0, "Timer Done" does not print.

    @IBOutlet var myTimer: WKInterfaceTimer!

    @IBAction func startButton() {
        myTimer.start()
        myTimer.setDate(NSDate(timeIntervalSinceNow: 4)) // Arbitrary 4 second coundown.

        // Impliment an alert.
        if myTimer == 0 {
            print("Timer Done")

        } else {
        print("Tick Tock")

    }
}
auth private
  • 1,318
  • 1
  • 9
  • 22
Dan O'Leary
  • 916
  • 9
  • 20
  • For starters, you're missing a closing curly brace at the end of your `else` statement. – aaplmath Jul 19 '15 at 18:00
  • From the WKInterfaceTimer documentation: *"To know when the timer reaches 0, configure an NSTimer object with the same target date you used to set up the timer"* – Martin R Jul 19 '15 at 18:06

2 Answers2

1

This is the solution I came up with. Right now the alert at the end of the timer simply prints to the console with "Done". Big thanks to user Prawn as I used some of his code in this solution.

import WatchKit
import Foundation

var myTimer : NSTimer?
var elapsedTime : NSTimeInterval = 0.0
var startTime = NSDate()
var duration : NSTimeInterval = 4.0 //Arbitrary 4 seconds to test timer.


class InterfaceController: WKInterfaceController {

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
    WKTimer.setDate(NSDate(timeIntervalSinceNow: duration))

}


override func willActivate() {
    super.willActivate()

}

@IBOutlet var WKTimer: WKInterfaceTimer! //The WatchKit timer the user sees

@IBAction func startButton()  { //Start button

    NSTimer.scheduledTimerWithTimeInterval(duration, target: self, selector: ("timerDone"), userInfo: nil, repeats: false)
    WKTimer.setDate(NSDate(timeIntervalSinceNow: duration ))
    WKTimer.start()

}


//Reset button resets the timer back to the original time set.
@IBAction func resetButton() {
    WKTimer.stop()
    WKTimer.setDate(NSDate(timeIntervalSinceNow: duration))

}


func timerDone() {
    print("Done")

}


override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}

}
Dan O'Leary
  • 916
  • 9
  • 20
  • Doesn't this only call the timerDone method if the watch is awake when the timer ends? How do you ensure that is called when the time runs out (which may be while the app with the NSTImer is asleep). – Scott Dec 30 '15 at 08:09
  • Hey Scott – wondering if you or Dan ever figured out how to ensure it is called when the app is asleep. I currently am trying to find a workaround. Specifically, I'm hoping to create some sort of timer that counts down from 3 to 0 and provides haptic feedback at every second interval. Unfortunately this is only working when the app is awake, as scheduledTimer() doesn't fire when the watch extension is asleep. – user2623825 Jan 13 '18 at 21:49
0

What you did is wrong. After you call setDate the timer start counting down. To get info what is actual state of the timer you should instantiate NSTimer and check when it fires.

From Apple's Documentation:

To know when the timer reaches 0, configure an NSTimer object with the same target date you used to set up the timer.

Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79