Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning screen off

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.

like image 247
Ilya Suzdalnitski Avatar asked Jul 27 '09 18:07

Ilya Suzdalnitski


2 Answers

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)]
like image 120
James Skidmore Avatar answered Oct 25 '22 06:10

James Skidmore


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.

like image 25
Tibidabo Avatar answered Oct 25 '22 07:10

Tibidabo