I'm making a macOS MenuBar app
only, so how would you hide the app's icon from the macOS dock when it's launched?
I haven't seen anywhere where this is explained nor found any SwiftUI APIs/interactions with the AppDelegate.
You need to set the following property in your Info.plist Application is agent (UIElement)
to YES
You can find it in the documentation here.
Or you can manually edit the Info.plist and add the following key:
<key>LSUIElement</key>
<true/>
You can read about it here
LSUIElement (Boolean - macOS) specifies whether the app runs as an agent app. If this key is set to YES, Launch Services runs the app as an agent app. Agent apps do not appear in the Dock or in the Force Quit window. Although they typically run as background apps, they can come to the foreground to present a user interface if desired. A click on a window belonging to an agent app brings that app forward to handle events.
The Dock and loginwindow are two apps that run as agent apps.
You can hide App's icon using Xcode Info
Tab's parameters. Here's an Example of how to do it.
However, implementing the following code you can hide app's icon in the Dock programmatically.
import SwiftUI
@available(macOS 13.0, *) @main struct YourApp : App {
var body: some Scene {
let _ = NSApplication.shared.setActivationPolicy(.prohibited)
MenuBarExtra("", systemImage: "apple.logo") {
Button("Quit") {
NSApplication.shared.terminate(nil)
}
}
}
}