SwiftUI アプリのライフサイクル iOS14 AppDelegate コードをどこに置くか? 質問する

SwiftUI アプリのライフサイクル iOS14 AppDelegate コードをどこに置くか? 質問する

および がAppDelegateSwiftUIから削除されたので、たとえばおよび にSceneDelegateあったFirebase 構成のコードはどこに置けばよいですか?SceneDelegateAppDelegate

現在、次のコードが私の中にありますAppDelegate:

このコードをどこに置けばいいでしょうか?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    FirebaseConfiguration.shared.setLoggerLevel(.min)
    FirebaseApp.configure()
    return true
}

ベストアンサー1

SwiftUIライフサイクルの解決策はこちらです。Xcode 12b / iOS 14でテスト済み

import SwiftUI
import UIKit

// no changes in your AppDelegate class
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print(">> your code here !!")
        return true
    }
}

@main
struct Testing_SwiftUI2App: App {

    // inject into SwiftUI life-cycle via adaptor !!!
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

おすすめ記事