SwiftUI:MacOS 上的 AppDelegate

发布于 2025-01-10 10:55:45 字数 560 浏览 0 评论 0原文

我正在将 SwiftUI iOS 应用程序移植到 macOS。它使用 @UIApplicationDelegateAdaptor 属性包装器来绑定 UIApplicationDelegate 类。不幸的是,UIApplicationDelegate 类在 macOS 上不可用,所以我想知道如何绑定我的自定义 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 {
        ...
    }
}

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 {
        ...
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2025-01-17 10:55:45

macOS 等效项具有 NS 前缀

import AppKit

class AppDelegate: NSObject, NSApplicationDelegate {

...

@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

,并且 DidFinishLaunching 委托方法具有不同的签名

func applicationDidFinishLaunching(_ aNotification: Notification) [ ...

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) [ ...
一绘本一梦想 2025-01-17 10:55:45

几乎是一样的,但是使用 NS 而不是 UI。我的做法与您略有不同:

@main
struct MyApp: App {
    
    // MARK: - Properties
    // Used variables
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    @Environment(\.openURL) var openURL
    
    var body: some Scene {
        WindowGroup {
            MainControlView()
        }
        .commands {
            FileMenuCommands(listOfContainers: listOfContainers)
        }
    }
}

然后您可以编写您的应用程序委托,如下所示:

import AppKit
import SwiftUI

class AppDelegate: NSObject, NSApplicationDelegate {
    // Whatever you want to write here
}

这一切都对我有用。

it's almost the same, but then use NS instead of UI. I did it slightly different than you:

@main
struct MyApp: App {
    
    // MARK: - Properties
    // Used variables
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    @Environment(\.openURL) var openURL
    
    var body: some Scene {
        WindowGroup {
            MainControlView()
        }
        .commands {
            FileMenuCommands(listOfContainers: listOfContainers)
        }
    }
}

Then you can write your application delegate like:

import AppKit
import SwiftUI

class AppDelegate: NSObject, NSApplicationDelegate {
    // Whatever you want to write here
}

This all worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文