Sleep/Lock despite UIApplication.shared.isIdleTimerDisabled

I have an app that records video (and also provides a custom remote interface) so it needs to remain awake and in the foreground.

It sets;

UIApplication.shared.isIdleTimerDisabled = true

I've also tried catching willEnterForegroundNotification to ensure it resets it if the app is backgrounded and resumes;

.onReceive(
  NotificationCenter.default.publisher(
    for: UIApplication.willEnterForegroundNotification)
  ) { _ in
    UIApplication.shared.isIdleTimerDisabled = true
  }

However, it seems that on some devices it will still go to sleep. This seems to be the case when Adaptive Power Mode is on (or rather, I've not managed to reproduce it when Adaptive Power Mode is off) even when battery percentage is well over 20% (I sort of expected Low Power Mode to trigger this)

Am I missing something obvious? there must be a way to make sure media capture apps stay awake (I'm surprised AVFoundation doesn't do it anyway!)

If it is related to Adaptive Power Mode, is there any way to detect that programatically to at least provide a warning to the user that having it on will affect operation of the app?

Thanks for this awesome post. Yes, low power will trigger the device to go night night if possible. This is a great post for other developers to provide tips and ticks how to keep the device awake on a recording session.

https://developer.apple.com/documentation/uikit/uiapplication/isidletimerdisabled

Your approach of setting is indeed the standard way to achieve this. However, certain power management features, like Adaptive Power Mode, can override these settings, as you've observed. Ensure that you have an active configured while recording. This typically helps in signaling to the system that media capture is in progress, which should naturally prevent the device from sleeping.

Unfortunately, iOS doesn't provide public APIs to directly detect Adaptive Power Mode specifically. However, you can check for Low Power Mode .

While this doesn't exactly map to Adaptive Power Mode, it can still be useful for alerting users. Since detecting Adaptive Power Mode isn't directly possible, consider displaying a message within your app that explains the importance of keeping the device awake for optimal performance and suggests disabling power-saving features while using the app.

Do you set the session?

let captureSession = AVCaptureSession()
// Configure and start your capture session
captureSession.startRunning()

Do you check if the low power mode is on?

if ProcessInfo.processInfo.isLowPowerModeEnabled {
    // Notify user that low power mode might affect app performance
}

Albert Pascual
  Worldwide Developer Relations.

Sleep/Lock despite UIApplication.shared.isIdleTimerDisabled
 
 
Q