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

Kun*_*yar 14 appdelegate swiftui

这是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()
                
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Phi*_*hov 32

您已标记您的应用程序,@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")
}
Run Code Online (Sandbox Code Playgroud)

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

如果出于某种原因您仍然想将它们放入您的委托中,您需要移至@mainAppDelegate初始化您的 SwiftUI 视图,UIHostingController就像在 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")
    }
}
Run Code Online (Sandbox Code Playgroud)

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