TheVacMaclesss
TheVacMaclesss

Reputation: 29

iOS Swift Firebase Checking user at app startup not working

I developed an app with firebase. The application checks to see if any users are logged in every boot in sceneDelegate.swift file. But its works only iOS 13 devices. iOS 12 devices users has to log in every time. How can I fix this problem? Thanks.

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 }
}

Upvotes: 2

Views: 334

Answers (1)

Frankenstein
Frankenstein

Reputation: 16341

SceneDelegate was introduced in iOS 13. For iOS 12 and below you should implement this same logic in the AppDelegate's didFinishLaunchingWithOptions method like this:

@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(withIdentifier: "navigationController") as! UINavigationController
            window?.rootViewController = navigationController
        }
        return true
    }
}

Upvotes: 4

Related Questions