While developing a Mac app as part of a broader project, we realized that we did not ever want our app to be hidden from the user. There are some pretty good reasons why we don’t want the user to ever forget that they have our app running. Ensuring that the app’s always visible and always on top, while a little annoying, ends up protecting our users’ privacy.
While you should probably never, ever need to do this in your own project, I’ve decided to share how I went about making this work seamlessly.
Scenario 1: Reacting to External Control
When a user is interacting with another app and selects “Hide Others” from the application’s menu, your app will automatically be hidden by the system. There are a number of events that you can listen to from the system that will give you an opportunity to force your application back on the screen, but the primary concerns are:
- You don’t want to be rude and steal focus from the currently active application when you unhide yourself.
- You don’t want your app to flicker off and back on the screen.
The winning combination, based on my testing on OS 10.7 – 10.9 (Developer Preview 1) is the following:
Now when your application is hidden from outside, it will simply refuse without any drama or flickering.
Scenario 2: Hiding from within Your Application
Technically, the above observer will unhide your application in the event the user hits ⌘H from within your app. However your app will be removed from the screen before it’s unhidden, resulting in a flicker. The solution here is pretty easy, though: go in your MainMenu.xib and simply delete the menu item for “Hide MyAwesomeApp”.
That’s it! It’s very simple. The only trick is exploring which APIs will get the little details right. And now you don’t need to. Please use this power wisely!