Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: AppDelegate on MacOS

Tags:

macos

swiftui

I'm porting a SwiftUI iOS app to macOS. It uses the @UIApplicationDelegateAdaptor property wrapper to bind a UIApplicationDelegate class. Unfortunately, the UIApplicationDelegate class isn't available on macOS, so I'm wondering how I bind my custom AppDelegate.

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
    ...
    }
}

struct MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        ...
    }
}
like image 616
G. Marc Avatar asked Jan 23 '26 19:01

G. Marc


1 Answers

The macOS equivalent has the NS prefix

import AppKit

class AppDelegate: NSObject, NSApplicationDelegate {

...

@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

and the DidFinishLaunching delegate method has a different signature

func applicationDidFinishLaunching(_ aNotification: Notification) [ ...
like image 167
vadian Avatar answered Jan 26 '26 11:01

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!