Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if iPhone screen is on/off

Tags:

iphone

ios4

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?

like image 241
venables Avatar asked May 24 '11 17:05

venables


People also ask

Why is my phone working but the screen is black iPhone?

If your iPhone is showing a black screen that never goes away, you should try resetting it. Black screens might appear if your iPhone overheats, encounters a major error, or the screen itself breaks. You should also plug your iPhone into a charger to make sure that it isn't simply out of battery.

What do I do if my iPhone screen turns?

Go to Settings > Accessibility. Select Motion, then turn on Reduce Motion.


1 Answers

Yes, there is no definitive method. UIApplication has a property protectedDataAvailable which will return YES when screen is unlocked and NO if 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 to UIApplicationProtectedDataDidBecomeAvailable and UIApplicationProtectedDataWillBecomeUnavailable notifications.

like image 144
Deepak Danduprolu Avatar answered Oct 18 '22 15:10

Deepak Danduprolu