我为Mac开发了一个Menu Extra(menulet),但我想知道使用Menu Extra的机器是否已经进入睡眠状态。我实现了一个贪睡功能,但因为它是一个基于时间的方法,所以它相当不可靠。任何提示或建议将不胜感激!
谢谢!
答案 0 :(得分:7)
您可能希望在NSWorkspace中查看NSWorkspaceWillSleepNotification
诀窍是,您需要在NSWorkspace的notificationCenter上注册通知,而不是默认通知。 See Technical Q&A QA1340。
示例代码:
- (void) receiveSleepNote: (NSNotification*) note
{
NSLog(@"receiveSleepNote: %@", [note name]);
}
- (void) fileNotifications
{
// These notifications are filed on NSWorkspace's notification center, not the default
// notification center. You will not receive sleep/wake notifications if you file
// with the default notification center.
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(receiveSleepNote:)
name: NSWorkspaceWillSleepNotification object:nil];
}