简体   繁体   中英

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.

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)]

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.

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.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM