I have searched quite a while for a fix to this problem, and it might have been that I have saw the solution to my problem, but did not understand it. Nevertheless, my problem is that I am trying to build an application in Swift that uses the MSAL Library to authenticate with a Microsoft account. I am stuck trying to link my "Login" Button in the Menu of the Statusbar Item to a function called "callGraphAPI()" inside of Microsoft's provided Library.
This is the code that I am referring to to build my App's presence in the Statusbar.
var lg = loginclass()
func buildstatusbaricon() {
statusBar = NSStatusBar()
statusBarItem = statusBar.statusItem(withLength: NSStatusItem.variableLength)
//define menu for status bar button
let button = statusBarItem.button
let buttonmenu = NSMenu()
let loginbutton = NSMenuItem()
//build the menu and items contained
loginbutton.title = "Login"
loginbutton.action = #selector(lg.callGraphAPI(_:))
loginbutton.target = self
entry.title = ""
buttonmenu.addItem(loginbutton)
buttonmenu.addItem(NSMenuItem.separator())
buttonmenu.addItem(entry)
//set the built menu as the menu of the button
statusBarItem.menu = buttonmenu
button!.image = NSImage(systemSymbolName: "globe", accessibilityDescription: nil)
}
The function callGraphAPI is located in a different class called loginclass , in a different file:
extension loginclass {
@objc func callGraphAPI(_ sender: Any) {
self.loadCurrentAccount { (account) in
guard let currentAccount = account else {
// We check to see if we have a current logged in account.
// If we don't, then we need to sign someone in.
self.acquireTokenInteractively()
return
}
self.acquireTokenSilently(currentAccount)
}
}
From what I understood, my problem has to do with the FirstResponder, but I am not sure how to interpret that. I wanted to build the app without a Storyboard, so many of the answers I found did not exactly make sense to me.
I would also like to add that the App is supposed to purely exist in the statusbar. When trying to use the example code from Microsoft, I could open a new NSWindow with my "Login" button from the status bar without any issues, and display the provided exemplary ViewController. Thus, I looked at how Microsoft's implementation of the UI looks, and found this:
// Add call Graph button
callGraphButton = NSButton()
callGraphButton.translatesAutoresizingMaskIntoConstraints = false
callGraphButton.title = "Call Microsoft Graph API"
callGraphButton.target = self
callGraphButton.action = #selector(callGraphAPI(_:))
callGraphButton.bezelStyle = .rounded
self.view.addSubview(callGraphButton)
That button is the "Login" button in the window, and to me it seems not that much different from how my statusbar button is created. The only difference is that my "Login" button in the status bar is not created in the same class. Is that the reason why my button won't work? I already tried adding the "target=self" or "target=loginclass.self" attributes, and the results stayed the same unfortunately.
I would appreciate any help with this issue.