Restore Backlight To Previous Level, iPhone

2019-03-22 08:08发布

问题:

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

回答1:

Discovered the answer. It was a really basic error... silly mistake. I had:

- (void)applicationWillTerminate

in ViewController.m rather than AppDelegate.m

It now works perfectly.

I hope this helps anyone out there with a similar problem,

Stu



回答2:

To controll the brightness within the app is an interesting idea. However, using this string will be a cause of rejection. (See the link.)

Well, ... I don't know how to restore the level after the user exits. Therefore, let me introduce two APIs to you. They are the way on Mac OS X.

CFPreferencesSetAppValue() and CFPreferencesAppSynchronize()

If you solve the problem, please write down the correct code for the other developers, include me. ;-)