iOS Swift Firebase在应用启动时检查用户不起作用

问题描述 投票:0回答:1

我用firebase开发了一个应用。该应用程序将检查是否有用户登录到sceneDelegate.swift文件的每次引导中。但它仅适用于iOS 13设备。 iOS 12设备用户必须每次登录。我该如何解决这个问题?谢谢。

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
@available(iOS 13.0, *)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    let currentUser = Auth.auth().currentUser

    if currentUser != nil {

        let board = UIStoryboard(name: "Main", bundle: nil)
        let navigationController = board.instantiateViewController(identifier: "navigationController") as! UINavigationController
        window?.rootViewController = navigationController

    }


    guard let _ = (scene as? UIWindowScene) else { return }
}
ios swift firebase firebase-authentication ios12
1个回答
0
投票

SceneDelegate在iOS 13中引入。对于iOS 12及以下版本,您应该在AppDelegatedidFinishLaunchingWithOptions方法中实现相同的逻辑,如下所示:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow()
        window?.makeKeyAndVisible()
        let currentUser = Auth.auth().currentUser

        if currentUser != nil {
            let board = UIStoryboard(name: "Main", bundle: nil)
            let navigationController = board.instantiateViewController(identifier: "navigationController") as! UINavigationController
            window?.rootViewController = navigationController
        }
        return true
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.