SwiftUI应用生命周期iOS14在哪里放置AppDelegate代码? [英] SwiftUI app life cycle iOS14 where to put AppDelegate code?

查看:119
本文介绍了SwiftUI应用生命周期iOS14在哪里放置AppDelegate代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,从SwiftUI中删除了AppDelegateSceneDelegate,我应该将以前用的代码放在SceneDelegate和AppDelegate的Firebase配置中吗?

Now that AppDelegate and SceneDelegate are removed from SwiftUI, where do I put the code that I used to have in SceneDelegate and AppDelegate, Firebase config for ex?

所以我的AppDelegate中目前有以下代码:

So I have this code currently in my AppDelegate:

我现在应该在哪里放置这段代码?

Where should I put this code now?

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
}

推荐答案

这是SwiftUI生命周期的解决方案.经过Xcode 12b/iOS 14的测试

Here is a solution for SwiftUI life-cycle. Tested with 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()
        }
    }
}

这篇关于SwiftUI应用生命周期iOS14在哪里放置AppDelegate代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆