How to block OSX logout and do some operation before it logoff

In mac osx when user logouts from it, I want to do certain action to make it graceful. I added my code in


func applicationWillTerminate(_ aNotification: Notification) {

// do some housekeeping

}

But my code is not completely executing, I want to block logout until my operation is complete.

Replies

You should implement the "applicationShouldTerminate" method in your app delegate:


https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428642-applicationshouldterminate


and return the value ".terminateLater". When your logout operation is complete, use "reply(toApplicationShouldTerminate:)" to tell the system you're done:


https://developer.apple.com/documentation/appkit/nsapplication/1428594-reply