iOS app will not auto-sleep

Hello, I am working on a iOS app. This is my first big project in SwiftUI which it is going well besides I can not get my phone to auto-sleep/auto-lock when I am in the app. I am testing the app on my phone through xcode. As I am checking my energy report on idle it is saying Low and is almost to the very bottom, and my memory is idling at 90MB~115MB which seems very low. Now If I just leave the app it works right away even if I do not clear the app. I am wondering if anyone has any ideas on how to help me with this issue.

I am using HealthKit, a Timer and the camera. I am making a workout app to give some information. I can also give more info if needed

I will appreciate any help or ideas. I have tried to use AI to see if it can find any issues and it said that it didn't see anything wrong with my code that will cause it not to sleep now that is AI and it isn't perfect so I would appreciate any help.

Thanks for your post and congratulations on your SwiftUI app.

Is the iPhone is plugged into your Mac and running through Xcode? The debugger alters the device's power management and prevents it from sleeping normally so you don't lose your debugging session.

There are a few things that prevents the device for going to sleep that you may be using without knowing. On top of my head. If you are using the camera, you likely have an AVCaptureSession running, make sure the camera session is not actively running, because it assumes the user is framing a shot or recording. Even if the camera view is hidden behind another SwiftUI view or you navigated to a different tab, if you didn't explicitly tell the session to stop, it keeps running in the background and keeps the screen awake. Temporarily comment out the code that calls session.startRunning() and see if the phone sleeps. If it does, you know the camera is the issue.

Another one, do a global search in your Xcode project for isIdleTimerDisabled Since you are building a workout app, you might have set this flag to true to keep the screen on while the user is exercising?

Looking for other developer’s ideas in this thread.

Albert
  Worldwide Developer Relations.

iOS app will not auto-sleep
 
 
Q