Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I observe changes to Night Shift on macOS / iOS?

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.

like image 406
Nate Avatar asked Aug 05 '17 18:08

Nate


1 Answers

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
like image 175
TheNextman Avatar answered Nov 03 '22 04:11

TheNextman