Search code examples
iosswiftxcodeappdelegatelaunching-application

application(_ application: UIApplication, didFinishLaunchingWithOptions) not called on start up


When I kill the app by swiping up in the multi-app UI in the simulator and relaunch it the application (didFinishLaunchingWithOptions) method is not called, and every time just the login screen shows up. I do not understand what's going on and it really defeats the purpose of checking whether user is logged in or not from firebase if the method is not even called while launching the app again, would really appreciate some help! (Does this have something to do with SceneDelegate methods I am seeing, from what i understand the didFinishLaunching method should be called regardless when launching an application)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        print("---------appDelegate didFinishLaunchingWithOptions called!---------------")
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = MainViewController()
        FirebaseApp.configure()
        return true
    } 

Here's the code for the MainViewController as requested

import UIKit
import Firebase

class MainViewController: UIViewController {

    var handle: AuthStateDidChangeListenerHandle?

    override func viewDidLoad() {
        super.viewDidLoad()
        DispatchQueue.main.async {
            self.handle = Auth.auth().addStateDidChangeListener { (auth, user) in
                if user == nil {
                    print("nil user -----------")
                    self.perform(#selector(self.showHomeController), with: nil, afterDelay: 3)
              } else {
                    print("non nil user --------")
                    self.perform(#selector(self.showWelcomeController), with: nil, afterDelay: 3)
              }
            }
        }
    }

    @objc func showWelcomeController () {
        present(WelcomeViewController(), animated: true, completion: nil)
    }

    @objc func showHomeController () {
        present(HomeViewController(), animated: true, completion: nil)
    }
}

The ---------appDelegate didFinishLaunchingWithOptions called!--------------- is printed only once, when the project is built and opened in simulator

Disclaimer: I'm very new in iOS app development.


Solution

  • The problem is just the way you "relaunch in the sim". If you kill the app and then tap the app's icon in the simulator, you are no longer running in Xcode; you are running independently. So you don't get any debugging any more; no print messages appear in the Xcode console, you don't stop at breakpoints, etc.

    The solution is: relaunch by telling Xcode to build and run again, not by tapping the app's icon in the simulator.