Detecting sleep/wake event in IOS

In IOS, when the device is kept idle for some time, the screen turns off and the device enters sleep mode. It also enters sleep mode when we press the power button to turn the screen off.

In my application, I wanted to detect If the device has entered/exited the sleep mode. I have followed the below links but some of these ways like observing 'Darwin notifications' is no longer allowed by apple. Other ways consider the device being locked as sleep mode, which is not precisely correct.

Is there a way to correctly determine this? Please share the apple documentation links if this is possible.

https://stackoverflow.com/questions/14191980/detect-screen-on-off-from-ios-service/14208787#14208787

https://nemecek.be/blog/104/checking-if-device-is-locked-or-sleeping-in-ios

Same problem here. There are some alternates like this, but I'd like to capture the actual event (if it exists).

There is no supported way to be notified of sleep and wake events on iOS.

Why do you need this?

The reason I ask is that the equivalent events on macOS are only useful because, on macOS, it’s possible to keep a process running continuous. In contrast, apps are typically suspended when they move to the background. You can use various techniques to prevent that — like a UIApplication background task — but those techniques also prevent the processor from sleeping. So your code can never be running to receive these events, so any mechanism wouldn’t be a notification but some sort of timeline of historical sleep and wake events.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Detecting sleep/wake event in IOS
 
 
Q