TurtleKurtze TurtleKurtze - 6 months ago 20
iOS Question

iPhone screen sleep - built in proximity sensor

Bon jour

is it possible to implement the following:

a. user places the iphone screen side down.
b. the screen sleeps (black)
c. the app reamins in foreground with full functionality

I believe other apps might achieve this with the proximity sensor?

Will this drain the battery significantly?

Cheers!

Answer

You can do:

[UIDevice currentDevice].proximityMonitoringEnabled = YES;
BOOL shouldDimScreen = [UIDevice currentDevice].proximityState;

if (shouldDimScreen) {
    // do whatever you want
} else {
    // light the screen back up
}

You can use NSNotificationCenter to register for the UIDeviceProximityStateDidChangeNotification if you'd like to know every time the status changes.

But, iOS will automatically turn off the screen for you if the user doesn't touch it for a while. So as long as you don't turn off the idle timer your battery life will be fine, and you might not need to implement this.