首页 > 解决方案 > 自动登录,AppDelegate 到 SceneDelegate,UIApplication.shared.delegate 一样!AppDelegate 被替换为?

问题描述

我在尝试设置自动登录功能时遇到问题。

我之前对根 ViewController 进行了一些研究,发现现在我需要使用 SceneDelegate 来实现自动登录功能,而不是使用 AppDelegate。但是,每当我尝试注销或登录时,以下代码都会使我的应用程序崩溃。

(UIApplication.shared.delegate as! AppDelegate).configureInitialViewController()

我知道我不再可以使用它,因为我已经从 AppDelegate 切换到 SceneDelegate。任何人都可以就我应该用来允许我登录和注销的修改后的代码提出建议吗?

我在 SceneDelegate 中的代码如下。

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    configureInitialViewController()
    guard let _ = (scene as? UIWindowScene) else { return }
}

func configureInitialViewController() {
    var initialVC: UIViewController
    let storyboard = UIStoryboard(name: "Welcome", bundle: nil)

    if Auth.auth().currentUser != nil {
        initialVC = storyboard.instantiateViewController(withIdentifier: IDENTIFIER_TABBAR)
    } else {
        initialVC = storyboard.instantiateViewController(withIdentifier: IDENTIFIER_WELCOME)
    }

    window?.rootViewController = initialVC
    window?.makeKeyAndVisible()
}

标签: iosswift

解决方案



推荐阅读