How to display NSOpenPanel from a GUI macOS app running as root

I use the NSOpenPanel to open a file picker dialog (default from macOS) to let user select a file to open. I use the following code:

let panel = NSOpenPanel()

panel.message = "Select file to open"

panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
panel.canChooseFiles = true

let resp = panel.runModal()
if(resp == .OK)
{
    let strFilePath = panel.url?.path();
    
    //do the work...
}
else if(resp != .cancel)
{
    //Error
}

This works. But if I run my GUI app as root (which I need for the purpose of setting up my launch daemon), the panel.runModal() returns 0, or .cancel without showing the dialog.

Any idea how do I make it work under root?

How to display NSOpenPanel from a GUI macOS app running as root
 
 
Q