Three operating modes of macOS application

Normal,You won't encounter this problem,Until you need to write a background program...

We all know that macOS has a global Dock,All running programs will be displayed on this thing,But if your program is a menu bar applet,Or a...input method,Then you definitely don’t want the icon of this app to be displayed on the Dock,Because this kind of app needs to run continuously in the background。

At this time we can write in Info.plist LSBackgroundOnly Field,This app will run in the background,No Dock icon,It will not be displayed in the switch option of cmd+tab。This is what the drop grid input method does,This is also the configuration in Apple's official example-but this has long been accompanied by a problem,That is if your app still needs to display the necessary settings interface,Then this interface will always be at the bottom of all windows,That is,The window was blocked as soon as it popped up。Even,In addition to standard windows,Any other warning popups,All can't be displayed,Unless a standard window appears first,And the user clicked anywhere in the window...

The above premises have caused many problems,I have to embed another app as a special setting software for the drop-off input method,Then use xpc communication between the two,Really desperate。

Until today TIL,I found out that it can be used NSApplication.shared.setActivationPolicy(.accessory) Dynamically change your run level in the program,You can dynamically modify yourself to be ordinary regular mode,Or backstage prohibited mode。Obviously,I also understand by looking at the code name,It turns out that this background mode,Is deliberately disallowing pop-up windows。

Obviously,If you declare that the program is a background program,Then all of its windows can't get the focus,I always thought this problem was a bug of macOS😅

Then,Is there any other plan? Really have,In addition to the two modes above,macOS actually provides a mode between the two,No icon is displayed like a background program,It can also get focus when necessary and pop-up windows like ordinary programs:

When necessary, the operating mode of the program is dynamically switched to accessory .。


of course,If the code is too troublesome,We can also directly change Info.plist,The LSBackgroundOnly ,To LSUIEelement ,.。

Original article written by Gerber drop-off:R0uter's Blog » Three operating modes of macOS application

Reproduced Please keep the source and description link:https://www.logcg.com/archives/3531.html

By R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

2 comments

Leave a comment

Your email address will not be published. Required fields are marked *