Home > Net >  How can I close/quit my app in macOS SwiftUI life cycle?
How can I close/quit my app in macOS SwiftUI life cycle?

Time:11-30

I want to know how can I close or quit my app? also I have some code for closing my app, but the code does not looks modern and updated to SwiftUI, do we got some new and better one?

import SwiftUI

@main
struct Work_Space_macOSApp: App {
    var body: some Scene {
        WindowGroup {

            VStack {

                Button("Close App") {
                    
                    // Do we have more SwifUI-ish code for closing App?
                    NSApplication.shared.keyWindow?.close()
                }
                
                Button("Quit App") {
                    // How can I quit the app?
                    
                }
                
            }
            .frame(width: 200, height: 200)
        }
    }
}

CodePudding user response:

To terminate the application:

NSApplication.shared.terminate(nil)

There are still many, many features of AppKit that SwiftUI doesn't support.

  • Related