Is there any way to detect if an iPhone’s screen is on or off? For example, when the phone’s screen lock button is pressed.
I’ve been using (void)applicationWillResignActive:(UIApplication *)application; to prepare for such events (which works fine for the most part), but this method is also fired for incoming calls, texts, etc.
As far as I can tell, there is no documented method to determine this.
I’ve been playing with some workarounds, like checking if screen resolution changed, checking if the orientation is unknown, or getting the brightness of the device. Nothing has panned out yet.
Does anyone have any creative/workaround solutions for this?
Yes, there is no definitive method.
UIApplicationhas a propertyprotectedDataAvailablewhich will returnYESwhen screen is unlocked andNOif locked only when user enables content protection. So this is the closest but unreliable I can think of. In such case, you can even listen toUIApplicationProtectedDataDidBecomeAvailableandUIApplicationProtectedDataWillBecomeUnavailablenotifications.