Shaul Hameed Shaul Hameed - 11 months ago 81
Swift Question

How to detect restart event in App delegate - OSX?

I want to quit the application only at the event of restart / logoff. Is there any way to find logoff/restart in OSX ?

Thanks in advance.

Answer Source

You can add an observer on NSWorkspace watching for NSWorkspaceWillPowerOff:

                                                    selector: #selector(yourMethod),
                                                    name: .NSWorkspaceWillPowerOff,
                                                    object: nil)

Here yourMethod will be called whenever the Mac is powering off (reboot or shutdown).

There's also notifications for going to sleep: NSWorkspaceWillSleep and for waking up: NSWorkspaceDidWake.