且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关闭窗口然后选择另一个应用程序时,Mac应用程序消失

更新时间:2023-10-13 11:22:22

此行为称为活动监视器中的进程列表真实反映了实际正在运行和未实际运行的情况.查看那里以确定您的应用是否真的终止了.

假定开发人员必须选择自动终止,因为它需要状态恢复的显式编码.但是,默认情况下,Xcode的应用程序项目/目标模板已启用它.您可以从Info.plist列表中删除NSSupportsAutomaticTermination键以将其禁用.

同样,如果您不准备支持突然终止",则可能也想禁用它.您将删除NSSupportsSuddenTermination键.

I created a new macOS project in Xcode 11.4 where the language is Swift and user interface is SwiftUI. Without making any changes to the code in the project, the app will disappear from the dock by performing the following steps:

  1. launch the Mac app by running the Xcode project
  2. close the main window of the Mac app
  3. select another running app such as Safari
  4. the original Mac app disappears from the dock with no way to access it

This behavior does not happen if the window of the Mac app is open. I can select other running apps then go back to the original Mac app with no problems.

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.