Loading...

How to execute some code before app termination in a macOS app using SwiftUI?

question swiftui macos
Ram Patra Published on September 4, 2024

To run some code before app termination in a macOS app using SwiftUI, the correct approach would involve placing the termination logic within a view, such as the ContentView. Here’s how you can do it:

Handling App Termination in a SwiftUI View

  1. Add a View Modifier in ContentView or Any Root View: You can use .onReceive within your main view to listen for the termination notification.

  2. Integrate Termination Logic into the Root View: The root view is the entry point of your app, where you can attach this logic.

Example Code:

import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onReceive(NotificationCenter.default.publisher(for: NSApplication.willTerminateNotification)) { _ in
                    // This code will be executed just before the app terminates
                    performCleanupBeforeExit()
                }
        }
    }

    func performCleanupBeforeExit() {
        // Perform any necessary cleanup before the app exits
        print("Performing cleanup before app termination.")
        // Insert your termination cleanup logic here
    }
}

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .padding()
    }
}

Explanation:

  • WindowGroup: The WindowGroup is used to define the main scene of your macOS application. It contains your root view, ContentView in this case.

  • .onReceive(NotificationCenter.default.publisher(for: NSApplication.willTerminateNotification)): This modifier is now attached to ContentView, which is the root view inside the WindowGroup. It listens for the NSApplication.willTerminateNotification, ensuring that your cleanup code runs just before the app terminates.

  • performCleanupBeforeExit(): This function contains the cleanup logic that you want to execute when the app is about to be terminated. This function is called within the .onReceive modifier.

Why This Works:

Attaching .onReceive to ContentView ensures that your app can respond to termination events correctly. Since ContentView is the main view within your WindowGroup, it will remain active for the duration of the app’s lifecycle, making it a suitable place to handle the termination event.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on September 4, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui macos October 15, 2023 How to display the app version and build number in a macOS/iOS SwiftUI app?

To display both the app version and build number in a SwiftUI macOS/iOS app, you can use the Bundle class to access information from the app’s Info.plist file. The Info.plist file contains various details about your application, including its version and build number. Here’s how you can do it:

question swiftui iOS April 2, 2024 How to open a second view from first view in iOS using SwiftUI?

In SwiftUI, you can open another view (or navigate to another view) on the click of a button by utilizing navigation views and navigation links. Here’s a basic example of how to achieve this:

question swiftui swift September 8, 2024 How to make Squircle shape in SwiftUI and how to easily convert it to a circle or a rectangle?

To create a squircle shape (a combination of a square and a circle, also known as a superellipse) in SwiftUI, you can define a custom shape by conforming to the Shape protocol and implementing the superellipse formula. The formula for a superellipse is: