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.

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 August 14, 2020
Image placeholder

Keep reading

If this article was helpful, others might be too

question macOS swift March 14, 2021 How to detect Escape key pressed in macOS apps?

Like Delete key, detection of Escape key press is also slightly different than detecting general key presses.

question macOS swift August 6, 2020 How to open an app's window on top of all others in Swift?

You can open your app’s window on top of all other open application windows with the below code:

question macOS swift November 10, 2021 How to detect fn key press in Swift?

You can override the flagsChanged() method of NSViewController and have your code like below to detect fn key press and release in macOS: