How to programatically detect backlight is on in iOS?

1.1k views Asked by At

I want iOS code that can show if the screen is lit or not, so that my app can monitor battery power drain due to the (power hungry) backlight activity. This code must use the public API only, as I intend to submit the app to the App Store eventually.

Initially I thought this may be simply a matter of checking the brightness property of the UIScreen, like so:

-(BOOL)isBackLightOn {
    float brightness = [UIScreen mainScreen].brightness;
    return brightness > 0;
}

but this is incorrect because [UIScreen mainScreen].brightness represents the value of the slider chosen by the user in the Settings->Brightness screen, not the current light output of the screen.

Apple clearly have a way to determine this value, since it is included in the energy diagnostics that can be viewed in Instruments (e.g. see Logging Energy Usage in an iOS Device), but maybe this measurement has not been exposed through the public API?

2

There are 2 answers

4
Flipper On

A question that may help yo with this is:

Changing Backlight Level, iPhone

It seems that if you include this:

#include "GraphicsServices.h"

And then using something like this:

NSNumber *bl = (NSNumber*) CFPreferencesCopyAppValue(CFSTR("SBBacklightLevel" ), CFSTR("com.apple.springboard"));
float previousBacklightLevel = [bl floatValue];

Should give you the backlight level. Remember, the screen is always going to have some form of backlight.

0
Hiren On

First of I have tried @Flipper's answer but it's not helped me a lot then I have R&D on this and i have found a solution with the below code

    NSLog(@"%f", [[[NSMutableDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.apple.springboard.plist"] objectForKey:@"SBBacklightLevel2"] floatValue]);

It's give me the float value, the current brightness level of the device