I am creating an app in which functionality like if a user is logged in, the app will go to home pages, otherwise it will go to login pages. So I’m adding the below code in AppDelegate to change the initial controller, but my code is not working.
let storyboard : UIStoryboard = UIStoryboard(name:"Main", bundle: nil)
let navigationController : UINavigationController = storyboard.instantiateInitialViewController() as! UINavigationController
let rootViewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
I want to change the initial controller if user is logged in. I want to achieve my requirement programmatically not using storyboard
>Solution :
You can solve your Problem by removing Scene Delegate, you can comment the whole class and then also comment the methods of Scene Delegate from App Delegate Class like :
/*
func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration",
sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication,
didDiscardSceneSessions
sceneSessions: Set<UISceneSession>) {
}
*/
and remove the Property Application Scene Manifest from info.pist
now run your code you will be able to change the initial Controller. actually when we have both classes then Scene Delegate will handle these things so by removing scene Delegate , AppDelegate will work