applicationWillTerminate not called

4.8k views Asked by At

I implemented applicationWillTerminate method, but it's never called

-(void)applicationWillTerminate:(NSNotification *)notification
{
    [[NSApplication sharedApplication] terminate:self];
    NSLog(@"EOP");
}

How to execute some code before application close?

Thanks

3

There are 3 answers

6
Ilya Blokh On BEST ANSWER

I just added

-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *) sender{
return YES;
}

Now applicationWillTerminate invokes

1
Vicky On

please set a key (UIApplicationExitsOnSuspend) in your plist to YES.

Key : UIApplicationExitsOnSuspend
Type : Boolean
Value :YES

0
Moose On

Solution 1:

Set the Application can be killed immediately to NO in the project info ( NSSupportsSuddenTermination key ). The applicationWillTerminate function will then be called before the application quits.

Solution 2:

If you need to be able to cancel the termination, then you should rather override the applicationShouldTerminate function

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
    // Do some stuff before quitting for good
    return .terminateNow
}