7

I need my application to be running without the iPhone going to sleep. But I'd like to turn the screen off. Something similar is done in the Phone application, when you speak on the phone.

I prevent the iPhone from going to sleep in the following way: [ [UIApplication sharedApplication] setIdleTimerDisabled: YES];

But how can I turn the screen off? And how do I turn it back, when user touched the screen?

Thanks.

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168

5 Answers5

5

Update: This method has been deprecated. See the comment by Timothée Boucher below.


You can turn the screen off via the proximity sensor, but there is no other public way to put the screen to sleep.

-[UIApplication setProximitySensingEnabled:(BOOL)]
James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • 2
    For anybody reading this, at the time of my comment, the method has been deprecated. From Apple's documentation: "Deprecated. The properties `proximityMonitoringEnabled` and `proximityState` of the `UIDevice` class are the replacements." – Timothée Boucher Feb 25 '10 at 00:21
2

Well, you can turn the brightness off completely. It does not lock the screen and the device still displays but no LCD backlight makes it almost impossible to see.

- (void) changeSystemBrightness: (NSString *) switchValue {

if ([[UIScreen mainScreen] respondsToSelector:@selector(setBrightness:)]) {
    if (switchValue) {
        if ([switchValue isEqualToString:@"saveDefault"]) {
            //
            self.userBrightness = [UIScreen mainScreen].brightness;
            //NSLog(@"User Brightness: %1.1f", userBrightness);
        } else if ([switchValue isEqualToString:@"restoreDefault"]) {
            [UIScreen mainScreen].brightness = self.userBrightness;
            //NSLog(@"Restore Brightness: %1.1f", userBrightness);
        } else if ([switchValue isEqualToString:@"min"]) {
            //[UIScreen mainScreen].brightness = 0;
        } else if ([switchValue isEqualToString:@"max"]) {
            [UIScreen mainScreen].brightness = 1;
        } else if ([switchValue isEqualToString:@"mid"]) {
            [UIScreen mainScreen].brightness = 0.5;
        }
    } else {
        [UIScreen mainScreen].brightness = self.userBrightness;
        //NSLog(@"Restore Brightness: %1.1f", userBrightness);
    }
}

}

First save user's system brightness level

[self changeSystemBrightness:@"saveDefault"];  

After that you can simply turn off the screen:

[self changeSystemBrightness:@"min"];  

Restore brightness:

[self changeSystemBrightness:@"restoreDefault"];  

iOS restores default system brightness once the screen is turned off normally (lock/unlock) so you have to detect and handle that.

Tibidabo
  • 21,461
  • 5
  • 90
  • 86
0

I can't confirm that that's a public functionality, but I know that there is a proximity sensor which can sense whether the phone is near your face or not. Try digging in and figuring out if that sensor is publicly available, and then which function might be turning the screen off.

TahoeWolverine
  • 1,749
  • 2
  • 23
  • 31
0

There is no official (public) programmatic way to turn the screen on or off, or even to change the brightness of the display. A few apps "fake" a brightness change by super-imposing a transparent black view on top of your view and changing its opacity to give the appearance of a change in brightness (the back-light will remain on, though, so it will never look like the screen is off and you won't save any battery).

Wilson
  • 751
  • 1
  • 7
  • 9
0

ya thair is the way you can use undocumented function GSEventSetBacklightFactor(1); this will make screen dim. if you replace 1 with 0 your screen will be off. then you have to press home button.for using this u have to import a priate framework graphicservice framework

  • It cannot be turn on with "GSEventSetBacklightFactor(1);" on a device with 3.1.2 framework. Even home button does not help, but only restarting device (Home+Sleep 10 seconds). It works on a the simulator. The device is uncracked. – slatvick Nov 07 '09 at 12:13
  • @slatvick - You mean `not jailbroken`, not `uncracked`. I hope it's not cracked. – Moshe Jul 12 '10 at 19:04