applicationWillResignActive and setBrightness not working?

眉间皱痕 提交于 2019-11-30 17:15:02
Tibidabo

I answered a similar question here: IOS5 setBrightness didn't work with applicationWillResignActive

iOS is not meant to retain in-app brightness values. It should restore system value after the app resigns active, quits, crashes etc. So officially there is no need to do that in applicationWillResignActive.

But it does't work. It's a bug. In fact it works if you try to switch to another app. Try pressing Home button twice and your brightness is gone.

Don't waste your time just file a bug report to Apple (I did well).

Unlock screen restores default system brightness. Just press the Power button twice and unlock to restore original brightness.

UPDATE: My bug report was closed because according to Apple it's not a bug. It is weird.

Since the current methods don't work in the app delegate methods, you can use a UIView as described by Adam.

Make a UIView lvar in the delegate h file:

UIView *view_;

Then in the implementation:

// create view in app delegate didFinishLaunchingWithOptions method and add it to the window
view_ = [[UIView alloc] initWithFrame:self.window.frame]; // delegate lvar
[view_ setBackgroundColor:[UIColor blackColor]];
[view_ setAlpha:0.5];
[self.window addSubview:view_];
[self.window makeKeyAndVisible];
return YES;

Make the view the size of your screen or view controller as needed. and make sure it's on top of all other subviews.

Keep in mind this will have a significant affect on the graphics performance of the app, but unless you are doing something crazy with the UI, it should be okay.

Have you tried registering your main view controller (or whatever object as to that) for the UIApplicationWillResignActiveNotification which is sent after executing applicationWillResignActive?

This will give you a chance to modify the brightness outside of applicationWillResignActive. Don't know if this method makes any difference, but trying it should be easy.

Simply call:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResign) name:UIApplicationWillResignActiveNotification object:nil];

then define:

- (void)appWillResign {
    [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:sysBright];        
}

Second to last line, you didn't assign the sysBright variable.

sysBright = [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
[[UIScreen mainScreen] setBrightness:sysBright];        
Suhail Patel

When you tap the home button your application doesn't resign active but rather goes into "Background Mode" and has its own delegate method which you need to define:

- (void)applicationDidEnterBackground:(UIApplication *)application

Something like this (in addition to the applicationDidBecomeActive: delegate method) should get the intended behavior:

- (void)applicationDidEnterBackground:(UIApplication *)application
{        
    sysBright = [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:sysBright]; 
}

did you call synchronize?

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