gpt4 book ai didi

objective-c - 后台应用程序 : NSAlert before shutdown

转载 作者:可可西里 更新时间:2023-11-01 02:20:05 32 4
gpt4 key购买 nike

我想要一个状态栏应用程序,它会在系统关闭/重启之前显示一个NSAlert。 (只是一个 NSStatusItem,没有窗口。)

它检测到关机并显示警报,但在用户可以与警报交互之前,应用程序终止然后系统关闭。

有没有办法强制系统在系统关闭之前等待警报完成?

编辑:如果应用程序显示在 Dock 中,代码确实有效,但如果它是 NSApplicationActivationPolicy.AccessoryUI 元素<,则代码无效。我试图在它退出之前将其恢复到 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) 来回发送电子邮件后,简短的回答是这是不可能的(对于 Developer Technical Support strong>LSUIElement/Agent 以捕获关闭/重启 进程):

引自 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.

我确实问过是否有任何“内部”方式。答案仍然是否定的。

关于objective-c - 后台应用程序 : NSAlert before shutdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31280638/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com