WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Using a timer when the watch goes in sleep mode

Forums > watchOS

Hi everybody,

I am trying to use a timer for an exercise app on Apple Watch (timer used for resting time after an exercise) but this timer stops when the watch goes in sleep mode (triggered by a wrist down). This is very unpractical as the user does the wrist down naturally during resting and the timer is stopped.

I am using a basic timer with publish and on receive: let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

.onReceive(timer) { _ in
                    if timeRemaining > -1 {
                        timeRemaining -= 1
                        withAnimation {
                            percent += 100 / totalSeconds

but it does not work whenever the user does a wrist down.

Can you please let me know how should I use the timer in order to work even in sleep mode? I see that the Apple Exercise app timer works even in sleep mode. Therefore, that should be doable.

Thank you in advance for your help


Hi, in general these timers are implemented in a way that you save the info that timer is running (+ start time) and then when app becomes active you can calculate the real elapsed time and update the UI.


Thanks for the answer. The only issue with this solution is when the timer goes to 0 and the watch is in sleep mode. I would like to give the user some haptic feedback when the timer goes to 0 (the resting time is over, it's time to go to the next exercise) whether the watch is active or in sleep mode. The apple exercise app seems to be working fine in sleep mode (timer + heartbeat recording are still running). Thus, I'd like to know how can I run some minimal code during sleep mode.

Thank you in advance for your feeback


Could you make the cool down a sepearte workout activity or a mindful activity? These are a few valid activities allowed in the background. Or schedule a local notification nSecs when the cool down should end?


Thanks, I think the notification will be the best approch.

For the count down, I used this code and it solves the problem perfectly. Thanks

        .onReceive(NotificationCenter.default.publisher(for: WKExtension.applicationWillResignActiveNotification)) { _ in
            print("Moving to the background")
            notificationDate = Date()
        .onReceive(NotificationCenter.default.publisher(for: WKExtension.applicationDidBecomeActiveNotification)) { _ in
            print("Moving to the foreground")
            let deltaTime: Int = Int(Date().timeIntervalSince(notificationDate))
            timeRemaining -= deltaTime
            percent += Double(deltaTime) / 1.2

For the notification, I used a simple test notification with a 5 second timer. It works perfectly when I quit the app before the 5 sec countdown, but not when I stay in the app with a wrist down (to put the watch in sleep mode)

func scheduleNotification() {
        let content = UNMutableNotificationContent()
        content.title = "Rest time is over"
        content.subtitle = "Be ready for the next set"
        content.sound = UNNotificationSound.default

        // show this notification five seconds from now
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

        // choose a random identifier
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

        // add our notification request

Should I use something different in order to make this notification work even if I stay in the app in sleep mode? I am not too familiar with the UserNotifications framework :(


@alexismoulin You can use WKExtendedRuntimeSession instead of UserNotification

Look at an example "How to make Apple Watch Haptic Engine continue playing in background" article.


Thanks @filimo ,

I will have a look at this article.


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.