I was working on my app recently and wanted to change the brightness of the backlight. I then wanted to restore the backlight level to it's original setting on exiting the app. Here is the code:
#include "GraphicsServices.h"
- (void) viewWillAppear:(BOOL)animated
{
NSNumber* bl = (NSNumber*) CFPreferencesCopyAppValue(CFSTR("SBBacklightLevel"), CFSTR("com.apple.springboard")); // To retrieve backlight settings
prevBacklightLevel = [bl floatValue];
GSEventSetBacklightLevel(0.5f);
}
// Other code here...
- (void)applicationWillTerminate
{
GSEventSetBacklightLevel(prevBacklightLevel); // To restore to original level
}
The backlight changes correctly on loading the app, yet when the user exits, the backlight remains at the level set by the app... how inconvenient!
Can any help me as to why this is not working as expected, am I doing anything obviously wrong?
I could not find much information on the web regarding this issue.
ViewController.h requires
#import "GraphicsServices.h"
also.
Edit // Can anyone help with this problem:
When app is closed backlight level does change, but always to 0, no matter what it was at before the app was run. Possibly bl is always 0 for some reason?
Many thanks,
Stu
Discovered the answer. It was a really basic error... silly mistake. I had:
in ViewController.m rather than AppDelegate.m
It now works perfectly.
I hope this helps anyone out there with a similar problem,
Stu