【发布时间】:2025-11-28 20:05:01
【问题描述】:
如何使用可可检测计算机何时即将关闭?互联网上似乎没有任何东西。这必须区分关闭和注销。
谁能帮我解决这个问题?
【问题讨论】:
标签: macos cocoa xcode4 osx-snow-leopard shutdown
如何使用可可检测计算机何时即将关闭?互联网上似乎没有任何东西。这必须区分关闭和注销。
谁能帮我解决这个问题?
【问题讨论】:
标签: macos cocoa xcode4 osx-snow-leopard shutdown
与 Matthias 提供的代码相同,但将通知名称更改为:
NSWorkspaceWillPowerOffNotification
如果你想防止系统关机,请添加
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
return NSTerminateCancel;
}
一定要使用“NSApplicationDelegate”
祝你好运!
【讨论】:
官方文档中的以下代码可能会对您有所帮助:
- (void) receiveSleepNote: (NSNotification*) note
{
NSLog(@"receiveSleepNote: %@", [note name]);
}
- (void) receiveWakeNote: (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: NULL];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self
selector: @selector(receiveWakeNote:)
name: NSWorkspaceDidWakeNotification object: NULL];
}
欲了解更多信息,请参阅:https://developer.apple.com/library/mac/#qa/qa1340/_index.html
【讨论】: