每当打开应用程序时,SwiftUI AppDelegate 都不会 运行

SwiftUI AppDelegate not being ran whenever app is opened

这是我制作的一个简单的AppDelegate

import SwiftUI

class AppDelegate: UIResponder, UIApplicationDelegate {

    private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        print("application")
        // first launch
        // this method is called only on first launch when app was closed / killed
        return true
    }

    private func applicationWillEnterForeground(application: UIApplication) -> Bool{
        print("applicationWillEnterForeground")
        // app will enter in foreground
        // this method is called on first launch when app was closed / killed and every time app is reopened or change status from background to foreground (ex. mobile call)
        return true
    }

    private func applicationDidBecomeActive(application: UIApplication) -> Bool {
        print("applicationDidBecomeActive")
        // app becomes active
        // this method is called on first launch when app was closed / killed and every time app is reopened or change status from background to foreground (ex. mobile call)
        return true
    }
}

@main
struct CouponDeckApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    @Environment(\.scenePhase) private var scenePhase
    var body: some Scene {
        WindowGroup {
            AppContentView()
                
        }
    }
}

不过,AppDelegate 似乎没有调用它的任何函数。 3处应该是运行他们,但是none是运行。为什么会发生这种情况,我该如何解决?

您已使用 @main 标记您的应用,这使其成为应用入口点。

并非所有应用程序委托的方法都适用于 UIApplicationDelegateAdaptor,像这里 applicationWillEnterForeground applicationDidBecomeActive 不会被调用。

相反,您应该在 SwiftUI 视图中使用发布者,如下所示:

.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
    print("applicationDidBecomeActive")
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
    print("applicationWillEnterForeground")
}

您的 AppDelegate 的另一个问题是它没有所需的方法签名。当您添加新方法时,不要从某个地方粘贴它,而是键入方法名称中的几个字母,然后让 Xcode 完成其余部分。在您的情况下,如果您的签名正确,private 将是 Xcode.

报告的错误

如果出于某种原因你仍然想让它们进入你的委托,你需要将 @main 移动到 AppDelegate,并使用 UIHostingController 初始化你的 SwiftUI 视图,就像它已经完成一样在 SwiftUI 1 中:

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let window = UIWindow()
        self.window = window
        window.rootViewController = UIHostingController(rootView: ContentView())
        window.makeKeyAndVisible()
        return true
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        print("applicationWillEnterForeground")
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        print("applicationDidBecomeActive")
    }
}

您还需要更新 Info.plist,将启用多个 windows 设置为 NO: