Restore Backlight To Previous Level, iPhone

a 夏天 提交于 2019-12-04 15:56:24

问题


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



来源:https://stackoverflow.com/questions/1786825/restore-backlight-to-previous-level-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!