NavigationController is isinitial viewcontroller in storyboard
, NavigationController embedded to LoginViewcontroller
in project storyboard navigation willbe like below
NavigationController->LoginViewcontroller-> RegistrationViewcontroller -> MainViewcontroller
in successful registration with PhNUmber i am getting userId
which i have stored in KeychainWrapper
in RegistrationViewcontroller: i am storing userId like below:
let userID: String=jsonObj?["userId"] as? String ?? ""
KeychainWrapper.standard.set(userID, forKey: "USERID")
which i am checking in appdelegate like below to go Mainviewcontroller:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var savedUserId: String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
print("appdelegate userid \(savedUserId)")
if savedUserId != nil{
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
}
return true
}
}
here savedUserId
coming then why i am not going to MainViewcontroller, all the time LoginViewcontroller appears
You need to initialize the window
. Give this line above setting the rootViewController
:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var savedUserId: String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
print("appdelegate userid \(savedUserId)")
if savedUserId != nil{
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window = UIWindow()
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
}
return true
}
}
Update: If you're using SceneDelegate
you should use this:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: windowScene)print("appdelegate userid \(savedUserId)")
if savedUserId != nil{
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window = UIWindow()
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
}
}
}