Loading...

How to quit or close an app in macOS using Swift?

question macOS swift
Ram Patra Published on August 14, 2020

You can quit or exit an app with:

NSApp.terminate(self)

If you’re making a macOS menu bar app in Swift and have an option in the dropdown menu to quit the app then your code would look something like:

NSMenuItem(title: "Quit", action: #selector(quitClicked), keyEquivalent: "q")

Menu Bar Item

@objc private func quitClicked() {
    NSApp.terminate(self)
}

Function invoked when a user clicks on the menu item

Note: Do not forget to add @objc in front of the function so that it can be called from #selector.

Ram Patra Published on August 14, 2020
Image placeholder

Keep reading

If this article was helpful, others might be too

May 28, 2020 Best Mac Apps for Working From Home

For me personally, it is a mixed feeling working from home. On one hand, I have the flexibility with when to work and when not to but on the other hand, it is sometimes hard for me to find that fine line between work and life. There are days when I keep working even after office hours without realizing that I have got my life wife too :)

question swiftui macOS January 26, 2024 How to hide the title bar in a SwiftUI macOS app?

Let’s say you have a view named ContentView and your main App file looks like this:

question presentify macOS August 6, 2020 How to annotate or draw in JioMeet?

Reliance Jio has done a commendable job releasing a video conferencing app in a short time frame, however, it received criticisms that it copied pixel by pixel from Zoom. I actually agree with some of the criticisms but that’s for another blog post.