1

当我强制我的 Mac 进入睡眠状态时,我成功获得了 NSWorkspaceWillSleepNotification:但是,如果我的计算机在能量偏好中指定的一段时间后自行进入睡眠状态,则不会发出此通知。

有解决方法吗?

4

1 回答 1

2

As mentioned in my comment, I've been able to reproduce this on some computers but not others. From a very quick investigation, it looks like 10.7.4 doesn't send the notification on idle sleep unless I've also called IORegisterForSystemPower, but 10.6.8 does. So, it could be a bug in 10.7. But, since my testing was pretty minimal, it could also be a bug in a specific hardware model, or something to do with strange settings that I didn't think of.

On the other hand, this could be intentional behavior—in which case I think there's a bug in the documentation. While it doesn't directly say that the notification is sent for both idle and forced sleep, but it certainly implies it.

The NSWorkspace notifications reference (can't get a URL because developer.apple.com seems to be down…) says it's "posted before the machine goes to sleep"—not "sometimes" or "only for forced sleep".

QA1340 (http://developer.apple.com/library/mac/#qa/qa1340/_index.html) discusses both kinds of sleep, and says that Cocoa can be used to receive notifications, without any hint that it only works for one kind. It gives sample code that should work, and that's what I used to reproduce the problem.

I'd strongly suggest filing a bug report with Apple, copying your bug report to http://openradar.appspot.com, and opening a thread on Apple's dev forums. And, if you're a paid MDP member, try it on 10.8DP4.1, and start a thread in the NDA section of the dev forums. If 10.8 is still broken, your radar might lead to Apple fixing it before the final release, while it's unlikely to be fixed if nobody reports it.

于 2012-06-20T17:42:09.643 回答