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

Prevent an Apple Watch app from sleeping on SwiftUI, not working

Forums > Swift


have tried this:

import UIKit


.onAppear {
    UIApplication.shared.isIdleTimerDisabled = true


ERROR: Cannot find 'UIApplication' in scope

and I am importing UIKit



This is basically because UIApplication does not exist on watchOS:


I'm not familiar with watchOS development, but if you describe what you want to achieve, perhaps somebody knows what's available on watchOS to do the same thing.


On WatchOS you need specific background modes enabled to prevent your app going to sleep. Or do you just mean that the screen shouldn't go black?



@Hatsushira, I just want the screen not to go black. When it does, the app goes to inactive. I want the app to continue active and running, as it does when I am debugging on the simulator.

I can detect the app going to inactive, as the screen goes black by using this

  @Environment(\.scenePhase) var scenePhase

    .onChange(of: scenePhase) { newPhase in
      if newPhase == .active {
      } else if newPhase == .inactive {
      } else if newPhase == .background {


I doubt this is possible if you don't use the special background sessions. But they're restricted to special use cases. With these the screen goes black anyway but the app still runs. I created a workout app for me and that was the way to go.





In apple watch as I'm using you need to apply such backgrounds that keep on running and consumes alot of battery inorder for you'r watch not to go to sleep mode.Thanx🥰.


Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.