macOS Swift UI App Life Cycle vs NSApplicationDelegate

Hi,

is it possible to have access to NSApplicationDelegate methods like
Code Block
application(_ sender: NSApplication, openFiles filenames: [String])
when using the Swift UI App Life Cycle?

With "access" I mean equivalent functionality. I know there is support for a document based app using the Swift UI App Life Cycle, which would seem to resolve the issue for that particular openFiles delegate method, but for various reasons I can't use the document based app life cycle.

Thank you!

Cheers, Michael
Answered by mjcotr in 631332022
Have you tried @NSApplicationDelegateAdaptor?
I've only tested it briefly with applicationDidFinishLaunching(), but that method does get called.

Code Block
@main
struct TestApp: App {
  @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {}
}
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        print("hello")
    }
}


Accepted Answer
Have you tried @NSApplicationDelegateAdaptor?
I've only tested it briefly with applicationDidFinishLaunching(), but that method does get called.

Code Block
@main
struct TestApp: App {
  @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {}
}
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        print("hello")
    }
}


(This should be a reply to the answer from mjcotr. But for some reason the reply does not seem to work as expected.)

Thank you, that looks exactly like what I was looking for. I haven't had heard about @NSApplicationDelegateAdaptor before.
I'll give it a try!

Thanks, Michael
macOS Swift UI App Life Cycle vs NSApplicationDelegate
 
 
Q