简体   繁体   English

applicationWillTerminate 未调用

[英]applicationWillTerminate not called

I implemented applicationWillTerminate method, but it's never called我实现了 applicationWillTerminate 方法,但从未调用过

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

How to execute some code before application close?如何在应用程序关闭之前执行一些代码?

Thanks谢谢

I just added我刚加

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

Now applicationWillTerminate invokes现在 applicationWillTerminate 调用

Solution 1:解决方案1:

Set the Application can be killed immediately to NO in the project info ( NSSupportsSuddenTermination key ).在项目信息( NSSupportsSuddenTermination键)中将应用程序可以立即NSSupportsSuddenTermination设置为 The applicationWillTerminate function will then be called before the application quits.然后将在应用程序退出之前调用applicationWillTerminate函数。

Solution 2:解决方案2:

If you need to be able to cancel the termination, then you should rather override the applicationShouldTerminate function如果您需要能够取消终止,那么您应该覆盖applicationShouldTerminate函数

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 .请将 plist 中的键 ( UIApplicationExitsOnSuspend ) 设置为YES

Key : UIApplicationExitsOnSuspend键: UIApplicationExitsOnSuspend
Type : Boolean类型:布尔型
Value :YES值:是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM