I want my OSX app to call a function when the user's screen becomes available, ex: their computer wakes from sleep or the user turns on their screen. Basically any time the user goes from having no screen active to having one active, I would like my function to be called.
我希望我的OSX应用程序在用户的屏幕可用时调用一个函数,例如:他们的计算机从睡眠状态唤醒或用户打开他们的屏幕。基本上每当用户从没有活动屏幕变为活动时,我希望我的功能被调用。
I'm not sure if the best way to do this is to:
我不确定这样做的最佳方法是:
- Check for changes in sleep/wake state or
- 检查睡眠/唤醒状态或
- Use CGDisplayReconfigurationCallback or
- 使用CGDisplayReconfigurationCallback或
- Listen for a NSWorkspaceScreensDidWakeNotification
- 侦听NSWorkspaceScreensDidWakeNotification
Which of these seems like the best way to do this, or should I do something else entirely? Some kind of example Swift code would be really helpful since snippets of code implementing any of these seem to be few and far between. Thanks.
其中哪一个似乎是最好的方法,或者我应该完全做其他事情?某种示例Swift代码非常有用,因为实现其中任何一个的代码片段似乎很少。谢谢。
2 个解决方案
#1
3
I was able to receive notifications for screen lock and screen unlock on OS X 10.10.5. Sorry I don't know Swift yet so here's the Objective-C:
我能够在OS X 10.10.5上接收屏幕锁定和屏幕解锁的通知。对不起,我不知道Swift,所以这里是Objective-C:
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(screenIsLocked) name:@"com.apple.screenIsLocked" object:nil];
[center addObserver:self selector:@selector(screenIsUnlocked) name:@"com.apple.screenIsUnlocked" object:nil];
- (void)screenIsLocked {
NSLog(@"screen is locked");
}
- (void)screenIsUnlocked {
NSLog(@"screen is unlocked");
}
#2
1
Well, first of all you have to test, whether you can get the solution for all situations with the different technologies. There are many situations that turns on the display (system restart, awake from sleep, connecting/disconneting screens, …) and I'm not sure, whether they are all triggered by all technologies.
那么,首先你需要测试,你是否可以使用不同的技术获得所有情况的解决方案。有很多情况会打开显示器(系统重启,从睡眠状态唤醒,连接/断开屏幕......),我不确定它们是否全部由所有技术触发。
However, as a general rule: You want to know, when the screen is activated. So use the notification that tells you that the screen is activated. This is NSWorkspaceScreensDidWakeNotification
. As long it works I would always use the technique that is closest to what I want.
但是,作为一般规则:您想知道,当屏幕被激活时。因此,请使用告知您屏幕已激活的通知。这是NSWorkspaceScreensDidWakeNotification。只要它有效,我总会使用最接近我想要的技术。
#1
3
I was able to receive notifications for screen lock and screen unlock on OS X 10.10.5. Sorry I don't know Swift yet so here's the Objective-C:
我能够在OS X 10.10.5上接收屏幕锁定和屏幕解锁的通知。对不起,我不知道Swift,所以这里是Objective-C:
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(screenIsLocked) name:@"com.apple.screenIsLocked" object:nil];
[center addObserver:self selector:@selector(screenIsUnlocked) name:@"com.apple.screenIsUnlocked" object:nil];
- (void)screenIsLocked {
NSLog(@"screen is locked");
}
- (void)screenIsUnlocked {
NSLog(@"screen is unlocked");
}
#2
1
Well, first of all you have to test, whether you can get the solution for all situations with the different technologies. There are many situations that turns on the display (system restart, awake from sleep, connecting/disconneting screens, …) and I'm not sure, whether they are all triggered by all technologies.
那么,首先你需要测试,你是否可以使用不同的技术获得所有情况的解决方案。有很多情况会打开显示器(系统重启,从睡眠状态唤醒,连接/断开屏幕......),我不确定它们是否全部由所有技术触发。
However, as a general rule: You want to know, when the screen is activated. So use the notification that tells you that the screen is activated. This is NSWorkspaceScreensDidWakeNotification
. As long it works I would always use the technique that is closest to what I want.
但是,作为一般规则:您想知道,当屏幕被激活时。因此,请使用告知您屏幕已激活的通知。这是NSWorkspaceScreensDidWakeNotification。只要它有效,我总会使用最接近我想要的技术。