Serdar Percin
Serdar Percin

Reputation: 5

"Remember User" in SignIn Screen does not direct to Main Screen in Swift. (window is nil)

After Sign In I want to remember user and should go to the main ViewController but it doesnt. I tried to check that my code works or not it seems work but it does nothing. I know it is working because when I write the string of "with Identifier", it gives error immediately but If I write "with Identifier" string wrong then it gives error. I put print to understand and it says "window is nil" all the time. By the way in app delegate I did:

var window: UIWindow?

Here is my code:

let user : String? = UserDefaults.standard.string(forKey: "username")
    
if user != nil {
        
    let board : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        
    let myTag = board.instantiateViewController(withIdentifier: "myTags") as! mainBeaconList
        
        
    print("mainBeaconList: \(myTag)")

    if let window = window {
        print("window: \(window)")
        window.rootViewController = myTag
    } else {
        print("window is nil")
    }
}

Upvotes: 0

Views: 36

Answers (1)

Ishteyaque Ahmad
Ishteyaque Ahmad

Reputation: 19

You might be missing to set window

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            self.window = UIWindow(frame: UIScreen.main.bounds)
let user : String? = UserDefaults.standard.string(forKey: "username")
    
    if user != nil {
        
        let board : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        
        let myTag = board.instantiateViewController(withIdentifier: "myTags") as! mainBeaconList
        
        
        print("mainBeaconList: \(myTag)")

        if let window = window {
            print("window: \(window)")
            window.rootViewController = myTag
        } else {
            print("window is nil")
        }
    
    }

Upvotes: 0

Related Questions