Shaul Hameed Shaul Hameed - 5 months ago 53
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.


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.