Detect if a brightness change on iOS was a user action or automatic action
Asked Answered
M

3

5

I've seen that I can detect changes in screen brightness by registering as an observer for UIScreenBrightnessDidChangeNotification

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(brightnessDidChange:) name:UIScreenBrightnessDidChangeNotification object:nil];
}

-(void) brightnessDidChange:(NSNotification*)notification
{
    NSLog(@"Brightness did change");
}

The object property of the notification is a UIScreen object.

I've tried to find a property that lets me know if the action was user generated or if it was an automatic change produced by iOS. This is important for my app since both situations should be treated differently. I did not find anything about this in the documentation. Any help will be appreciated.

Metastasize answered 15/1, 2014 at 11:17 Comment(0)
S
6

I think it is not possible, we can only get notification when Brightness change by the system or manually. we can't differentiate both.

Suggest answered 15/1, 2014 at 11:46 Comment(0)
V
8

If the user changes the brightness, they'd do it from the Control Center or from the Settings app. You could detect and even differentiate between these two based on UIApplicationDelegate notifications (as your app will go Inactive for when Control Center is opened, and into Background when the user opens Settings).

If you receive UIScreenBrightnessDidChangeNotification whilst your app is Active, you can be sure it was an automatic change triggered by the system.

Of course an automatic change could also happen whilst your app is in Inactive or Background states too, but maybe a heuristic solution based on this is better than nothing.

Vas answered 22/6, 2016 at 11:41 Comment(0)
S
6

I think it is not possible, we can only get notification when Brightness change by the system or manually. we can't differentiate both.

Suggest answered 15/1, 2014 at 11:46 Comment(0)
C
5

Swift 5.2

The notification is now called UIScreen.brightnessDidChangeNotification.

let noteCenter = NotificationCenter.default
noteCenter.addObserver(self,
                       selector: #selector(brightnessDidChange),
                       name: UIScreen.brightnessDidChangeNotification,
                       object: nil)

@objc func brightnessDidChange() {
    print(UIScreen.main.brightness)
}
Containment answered 20/8, 2019 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.