How to implement custom NSApplication terminate: behavior in Cocoa?

I am trying to implement custom completion behavior in a Cocoa application. Normally, when my application exits gracefully, it cleans up the final version database and then exits. This happens inside the AppDelegate (delegate NSApplication) when called [NSApp terminate:aSender]:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    // database cleanup...
    return NSTerminateNow;
}

If an error occurred during execution (for example, the database file was deleted), I present the error to the user and give them the option to Restore (return the file and try again) or exit. If Quit is selected, I want the application to skip database cleanup completely, as this has become impossible. In essence, I would like something like this:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    BOOL gracefulTermination = ...;

    if (gracefulTermination == YES)
    {
        // Database cleanup....
    }

    return NSTerminateNow;
}

, , gracefulTermination.

NSApp, terminate:, , infoDict, applicationShouldTerminate:?

, ?


, terminate: - , :

  • [NSApp terminate:self]; foo (a.k.a. self).
  • NSApp : [aDelegate applicationShouldTerminate:self]; ( NSApp, foo).
  • aDelegate applicationShouldTerminate:, .

foo, , - , ​​ , aDelegate , , NSApp . infoDict foo, infoDict, aDelegate, terminate:.


, [NSApp terminate:...] - exit(). , , , Cocoa. , applicationShouldTerminate:, , .

+3
1

, , , .

, - (, , ), : NSApplication, terminationInfo terminate:, super.

+3

Source: https://habr.com/ru/post/1758055/


All Articles