Search code examples
swiftwatchkitwatchos

How to create a stopwatch (timer) on Apple Watch (iOS)


Is there a way to create a timer that performs just like the Fitness app on watchOS or Clock app on iOS? More specifically, is there a dedicated SDK that lets you create timers with Swift? What's the suggested approach to doing that?

I know about the Timer class, but Timer only lets you listen for one-second changes, while the above-mentioned apps both have milliseconds timer.

Thanks!


Solution

  • You can use Timer and set the TimeInterval which really is in seconds, but which is also an alias for double.

    typealias TimeInterval = Double

    This means you can put there value such as 0.02. Also remember, that timer might not be triggered precisely at the interval. There is a

    var tolerance: TimeInterval

    • The amount of time after the scheduled fire date that the timer may fire.

    that can be configured for a timer instance.

    For more info see https://developer.apple.com/documentation/foundation/timer