How do you catch an application exit on Mac?

AppDelegate::applicationDidEnterBackground() doesn’t get called when closing an application on Mac OS X via Cmd+Q.

So how do I catch an application exit?

You’ll probably have to hook into implementing this yourself with GLFW or ObjC until the dev team adds a callback for quit?

That’s what I thought.
Just wanted to make sure, I’m not missing something obvious here.
Thanks for your quick reply.

If you have experience passing data back and forth or otherwise can do it in ObjC then I would just use applicationWillTerminate: for now in your AppController.mm. It will be simple for you or the dev team to add in a callback when this occurs, that then is called for any platforms supporting the concept.

I’m confused. There is no AppController.mm for Mac OS X, only for iOS…

I ended up inserting

#if CC_TARGET_PLATFORM == CC_PLATFORM_MAC
    Application::getInstance()->applicationDidEnterBackground();
#endif

into Director::end() which worked fine for my use case.

Thanks again @stevetranby

I am confused by why you would want to call applicationDidEnterBackground when the application is actually terminating.

Because that’s where my clean up code is. :wink:

And it’s an easily accessible callback from within my actual game.

It’s a hack of course, but it’s working for now.

Dang, and here I thought I was helping, doh! Yep that was iOS, sorry I was a little too hasty thinking it was correct without giving it more thought.

You could run your terminate code when your scene is cleaned up or reset?

Maybe I should submit a github PR, but then I guess not many people have needed it. Maybe scene cleanup is the better method?

// CCApplication-mac.mm
if (glview->isOpenGLReady())
{    
    // this could go last, or after this block
    applicationDidTerminate(); // not supported yet
    director->end();
    director->mainLoop();
}

Well, for me it’s working the way I did it. The data I want to save isn’t in any scene. It’s in a singleton (yes I know, I know, bad pattern… :wink: )

Calling applicationDidEnterBackground() from Director::end() is certainly dirty, I know… but it’s just for convenience. I’m not planning to release for Mac. It just for testing my game…
And it happened several times, that I closed my game with Cmd-Q and game data didn’t get saved…
I just needed a quick’n dirty fix.

But I think for people who want to release for mac, it could be an important thing.
I, for my part, would appreciate if you could spare the time to submit a PR.

Thx a lot for your effort.

Sounds good, I think I’ll use this to learn the process of finally actually submitting a PR :smiley:

For any others who read this who prefer to leave the engine alone: The scene callbacks could be used where one calls singleton::instance()->saveDataOnQuit() in all scenes or only scenes that change the data.