Mac app disappears when window is closed then another app is selected
This behavior is known as Automatic Termination. I find it a misfeature, but Apple considers it a feature.
Your app may not have actually quit. It may just appear to have quit. "Launching" it again will just make it reappear in the Dock. It's also possible that some apps which look like they're still running have actually been terminated by the system. In theory, if you try to switch to them, they will be launched and told to restore their previous state to maintain the illusion that they were running all along. In practice, apps (even Apple's) rarely properly restore things to exactly how they were.
The process list in Activity Monitor is a true reflection for what is and is not actually running. Look there to determine if your app has really been terminated.
A developer is supposed to have to opt-in to Automatic Termination because it requires explicit coding of state restoration. However, Xcode's app project/target templates have it enabled by default. You can remove the NSSupportsAutomaticTermination
key from your Info.plist to disable it.
Likewise, you'll presumably want to disable Sudden Termination, too, if you're not prepared to support it. You would remove the NSSupportsSuddenTermination
key.
Macos app is getting closed when there're no windows open
Set NSSupportsAutomaticTermination to NO in your Info.plist.
Why does my MacOS app disappear on the dock when run/build on Xcode?
Found the issue. When I created another target, it was the same name as the MacOS file except a capitalized letter on it. Seems that it confused git and overwritten the MacOS file
Crash when closing window in a MacOS application?
The problem is the window is automatically released (and thus deallocated) upon closure. This, combined with the automatic reference counting, presumably creates a sort of double free error. To solve this problem without disabling ARC or disabling releaseWhenClosed
, Window
is made a global or instance variable. Doing so will prevent ARC from releasing the window after already having been released by being closed.
NSWindow *Window;
// ...
- (void)applicationDidFinishLaunching:(NSNotification *)Notification {
Window = [[NSWindow alloc] initWithContentRect:(NSRect){.size = {800, 512}} styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:YES];
[Window center];
[Window makeKeyAndOrderFront:Window];
// Insert code here to initialize your application
}
OS X design decisions. Terminate the app on last window close?
Per Apple's Human Interface Guidelines (a guide for Mac developers):
In most cases, applications that are
not document-based should quit when
the main window is closed. For
Example, System Preferences quits if
the user closes the window. If an
application continues to perform some
function when the main window is
closed, however, it may be appropriate
to leave it running when the main
window is closed. For example, iTunes
continues to play when the user closes
the main window.
Related Topics
How to Programmatically Scroll iOS Wkwebview, Swift 4
Programmatically Select All Cells in Tableview So Next Time They Are Pressed They Call Diddeselect
How to Hide The Top Bar (With Buttons) Usin Swift and Macos
Swift: How to Get Image Name from Assets
Create Skspritenode with an Asset Programmatically
Generic Vector with Cardinality Type Safety
Uicollectionview + Nsfetchedresultscontroller in Swift 3
Wkwebview on Macos Cuts Off Top
Convert Gregorian Date to Hijri Date
Why Run Loop Is Needed When Using Dispatchqueue.Main.Async in MAC Command Line Tool in Swift
How to Count Number of Sprites Swift
Trim Video Always Fail When Use Avassetexportpresetpassthrough
Struct Hash for Different Types
Add Accessory to Home Always Returns Error(Genericerror)
Uibutton State Changing While Scrolling The Tableview with Multiple Sections - Swift
How Pass Data from Button in Tableviewcell to View Controller