macOS Xcode 14.2 app applicationwillterminate function is not called

I want to put some code to be run when my app terminates but the following:

       func applicationWillTerminate(_ aNotification: Notification) {
            // Insert code here to tear down your application
            print("Termination Code")
        }

in the AppDelegate class is not called, any ideas how to fix this issue.

I saw this posts https://developer.apple.com/forums/thread/126418 but I do not have an info.plist. I think the info.plist is not longer needed.

The following doc does not have much to say about this?? https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428522-applicationwillterminate

macOS apps definitely still have Info.plist files, but you may not be directly editing it. Rather, it may be generated based on settings under the Info tab in your target settings, and values in the Info.plist Values section under Build Settings. I would agree with that other thread that you should look for a plist setting that allows "sudden termination".

After reviewing some of the doc mentioned above, I did not see  "sudden termination" but I added "Application can be killed immediately when user is shutting down or logging out" setting to NO to the target using the INFO tab but still applicationWillTerminate function is not being called.

Any ideas how to fix?

The docs on NSApplicationWillTerminateNotification say it is "Posted only if the delegate method applicationShouldTerminate: returns YES". Do you implement that method?

I got it working. I did need to set Application can be killed immediately when user is shutting down or logging out" setting to NO to the target using the INFO tab but I had a coding error implementing applicationWillTerminate. So, here is how my app delegate looks now.

import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
   
    func applicationWillFinishLaunching(_ notification: Notification) {
    }
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application  
        //    try! DatabaseManager.setup(for: NSApp)
    }
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true
    }
    func applicationWillTerminate(_ Notification: Notification) {
        // Insert code here to tear down your application
        getPrefs()
        if paymePref.prefAutoBackup == "Yes" {
            backupDataBase()
        }
    }
}
macOS Xcode 14.2 app applicationwillterminate function is not called
 
 
Q