Mac app, SwiftUI: Change Dock menu

Hello.

I'm trying to change my SwiftUI Mac app icon's menu in the Dock, using the NSApplicationDelegate's applicationDockMenu(_ : ) function.

However, it does not work: The delegate function is only called once, randomly after launch, and then never again, and it will not show any items in that menu I return. When I right-click the app in the Dock, only the macOS-supplied items are shown, and my App delegate function is not called.

Here's the code I use inside my NSApplicationDelegate:

func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
		let men = NSMenu()
		
		print("applicationDockMenu called")

		var it = NSMenuItem(title: "Test1", action: #selector(test(_:)), keyEquivalent: "")
		it.target = self
		men.addItem(it)
		it = NSMenuItem(title: "Test2", action: #selector(test(_:)), keyEquivalent: "")
		it.target = self
		men.addItem(it)
		
		return men
	}
	
	@objc func test(_ sender: NSMenuItem) {
		print("application dock menu custom item called")
	}

Is there a SwiftUI modifier I should be using instead of the NSApp delegate method, or is this just not supported at this time in SwiftUI Mac apps?


Thank you,

- Matthias

Answered by m.gansrigler in 784251022

Alright, here's an update. It does work, after all. Just not when launched from Xcode. When you launch it manually from Finder, though, the items in the Dock's menu will show up and work. My thanks to Tom Swift over on Mastodon for letting me in on this secret.

This is what I mean, just in case I wasn't clear:

Accepted Answer

Alright, here's an update. It does work, after all. Just not when launched from Xcode. When you launch it manually from Finder, though, the items in the Dock's menu will show up and work. My thanks to Tom Swift over on Mastodon for letting me in on this secret.

Mac app, SwiftUI: Change Dock menu
 
 
Q