I'm looking for a way to determine when Night Shift has been enabled or disabled and perform an action based on that. I'm currently using the CBBlueLightClient header in the CoreBrightness framework to control Night Shift. Here's a partial header I'm using in my app:
@interface CBBlueLightClient : NSObject
- (BOOL)setStrength:(float)strength commit:(BOOL)commit;
- (BOOL)setEnabled:(BOOL)enabled;
- (BOOL)getStrength:(float*)strength;
- (BOOL)getBlueLightStatus:(struct { BOOL x1; BOOL x2; BOOL x3; int x4; struct { struct { int x_1_2_1; int x_1_2_2; } x_5_1_1; struct {
int x_2_2_1; int x_2_2_2; } x_5_1_2; } x5; unsigned long x6; }*)arg1;
@end
CBBlueLightClient also has a notification block, -
(void)setStatusNotificationBlock:(id /* block */)arg1;
which I can't figure out how to use.
Here's the full header for iOS. Everything I've tried works with macOS, including the notification block which seems to be there. I just can't figure out what kind of closure it's expecting.
You can just create a void
notification block, and it will fire when night shift is enabled / disabled and when the night shift settings are changed. Inside the notification block you can query whether night shift is enabled or disabled.
CBBlueLightClient *client = [[CBBlueLightClient alloc] init];
void (^notificationBlock)() = ^() {
StatusData status;
[client getBlueLightStatus:&status];
//... check status.enabled, status.active, etc);
};
[client setStatusNotificationBlock:notificationBlock];
It seems that the strength changing doesn't fire a notification. You could poll for that if you need to? It really depends what your use case is.
If you just want to know when it's been toggled, subscribe the notification as above and check the .enabled
property.
The header I'm using:
@interface CBBlueLightClient : NSObject
typedef struct {
int hour;
int minute;
} Time;
typedef struct {
Time fromTime;
Time toTime;
} Schedule;
typedef struct {
BOOL active;
BOOL enabled;
BOOL sunSchedulePermitted;
int mode;
Schedule schedule;
unsigned long long disableFlags;
} StatusData;
- (void)setStatusNotificationBlock:(id /* block */)arg1;
- (BOOL)getBlueLightStatus:(StatusData *)arg1;
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With