后台应用程序:关闭前的 NSAlert
Background Application: NSAlert before shutdown
我想要一个 状态栏 应用程序,它将在系统 shutdown/reboot 之前显示 NSAlert
。 (只有 NSStatusItem
,没有 window。)
它检测到关机并显示警报,但在用户可以与警报交互之前,应用程序终止然后系统关闭。
有没有办法强制系统在系统关闭之前等待警报完成?
编辑: 如果应用程序显示在 Dock 中,代码确实有效,但如果它是 NSApplicationActivationPolicy.Accessory
或 UI Element
,则代码无效。我试图在它退出之前将它恢复回 Dock,但在它再次弹出之前它就被终止了。
代码示例如下:
func receivedPowerOffNotification(notification: NSNotification) {
let myAlert: NSAlert = NSAlert()
myAlert.alertStyle = NSAlertStyle.CriticalAlertStyle
myAlert.messageText = "Title"
myAlert.informativeText = "Please wait for this alert"
myAlert.addButtonWithTitle("OK")
myAlert.addButtonWithTitle("Not OK")
let response = myAlert.runModal()
if response != NSModalResponseOK {
// Do something here before shutdown the system.
}
}
NSApplication.sharedApplication().replyToApplicationShouldTerminate(true)
}
func applicationShouldTerminate(sender: NSApplication) -> NSApplicationTerminateReply {
// userQuit = if the termination was caused by the Quit menu.
return userQuit ? NSApplicationTerminateReply.TerminateNow : NSApplicationTerminateReply.TerminateLater
}
好的,在与 Apple 的开发人员技术支持 (DTS) 来回发送电子邮件后,简短的回答是不可能(对于 LSUIElement/Agent 来捕获 shutdown/reboot 过程):
Apple 员工引述:
The rules change in AppKit for apps that are user-agent based. They
are designed not to interact with the user, even through our
documentation states you can add windows. Since your app has an
NSStatusItem, which constitutes a UI, it should not be user-agent
based. You are interacting with the user in this case.
我确实问过是否有任何“内部”方式。答案还是没有。
我想要一个 状态栏 应用程序,它将在系统 shutdown/reboot 之前显示 NSAlert
。 (只有 NSStatusItem
,没有 window。)
它检测到关机并显示警报,但在用户可以与警报交互之前,应用程序终止然后系统关闭。
有没有办法强制系统在系统关闭之前等待警报完成?
编辑: 如果应用程序显示在 Dock 中,代码确实有效,但如果它是 NSApplicationActivationPolicy.Accessory
或 UI Element
,则代码无效。我试图在它退出之前将它恢复回 Dock,但在它再次弹出之前它就被终止了。
代码示例如下:
func receivedPowerOffNotification(notification: NSNotification) {
let myAlert: NSAlert = NSAlert()
myAlert.alertStyle = NSAlertStyle.CriticalAlertStyle
myAlert.messageText = "Title"
myAlert.informativeText = "Please wait for this alert"
myAlert.addButtonWithTitle("OK")
myAlert.addButtonWithTitle("Not OK")
let response = myAlert.runModal()
if response != NSModalResponseOK {
// Do something here before shutdown the system.
}
}
NSApplication.sharedApplication().replyToApplicationShouldTerminate(true)
}
func applicationShouldTerminate(sender: NSApplication) -> NSApplicationTerminateReply {
// userQuit = if the termination was caused by the Quit menu.
return userQuit ? NSApplicationTerminateReply.TerminateNow : NSApplicationTerminateReply.TerminateLater
}
好的,在与 Apple 的开发人员技术支持 (DTS) 来回发送电子邮件后,简短的回答是不可能(对于 LSUIElement/Agent 来捕获 shutdown/reboot 过程):
Apple 员工引述:
The rules change in AppKit for apps that are user-agent based. They are designed not to interact with the user, even through our documentation states you can add windows. Since your app has an NSStatusItem, which constitutes a UI, it should not be user-agent based. You are interacting with the user in this case.
我确实问过是否有任何“内部”方式。答案还是没有。