I am trying to lead the user directly to the main page of my application if they are logged in (checked using user defaults).
However, it will stay at the screen I chose as the initial view controller in the storyboard no matter what. This is my didFinishLaunchingWithOptions function in the AppDelegate file:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
let db = Firestore.firestore()
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true))
if UserDefaults.standard.string(forKey: "userEmail") != nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarController")
let navigationController = UINavigationController(rootViewController: initialViewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
return true
}
>Solution :
From iOS 13 on, the responsibilites of AppDelegate have been split between AppDelegate and SceneDelegate. In particular, SceneDelegate is where you want to present a UIViewController.
Remove the if block of your code from AppDelegate and add it to wilConnectTo of SceneDelegate, like this:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
if UserDefaults.standard.string(forKey: "userEmail") != nil {
self.window = UIWindow(windowScene: windowScene)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(identifier: "TabBarController")
let navigationController = UINavigationController(rootViewController: initialViewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
}