2

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

1 Answer 1

4

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
    }
}
2
  • I added same code to appDelegate's didFinishLaunchingWithOptions function but, Xcode shows alert "instantiateViewController(identifier:creator:)' is only available in iOS 13.0 or newer" how can I fix this? Jun 13, 2020 at 13:58
  • Use the instantiateViewController(withIdentifier: method on board. Jun 14, 2020 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.