Question

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

Était-ce utile?

La solution

I just added

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

Now applicationWillTerminate invokes

Autres conseils

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
}

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

Key : UIApplicationExitsOnSuspend
Type : Boolean
Value :YES

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top