Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ViewController login page for different accounts does not work

Question 1. In my FIREBASE data, I hope that different values ​​of "Safety-Check" lead to different VIEWs.
Here is my code,
Can someone give me some advice?

 Database.database().reference().child("ID/\(self.uid)/Profile/Safety-Check").observeSingleEvent(of: .value, with: {
                                (snapshot) in
                    let cheak = "Safety-Check"
                    if cheak == "admin" {
                        
                        if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "AdminVC") {
                          UIApplication.shared.keyWindow?.rootViewController = viewController
                            self.dismiss(animated: true, completion: nil)
                    }
                    if cheak == "ON" {
                        if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "MainTabBarController") {
                          UIApplication.shared.keyWindow?.rootViewController = viewController
                            self.dismiss(animated: true, completion: nil)
      
                    }else{
                        if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SignUpViewControllerID") {
                          UIApplication.shared.keyWindow?.rootViewController = viewController
                            self.dismiss(animated: true, completion: nil)
                        }
                    }
                    }
                    }
                })
            })
    }
}

Question 2. Yellow Warning: ‘keyWindow’ was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes.

How should I modify it to fix this problem?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

UIApplication.shared.keyWindow?.rootViewController = viewController

>Solution :

Use below function the get the window :

func GetWindow() -> UIWindow? {
    
    if #available(iOS 13.0, *) {
        let sceneDelegate = UIApplication.shared.connectedScenes
            .first?.delegate as? SceneDelegate
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        return sceneDelegate?.window ?? appDelegate?.window
    } else {
        
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        return appDelegate?.window
    }
}

usage of above function :

 if let window = GetWindow() {}

With respect to your case :

GetWindow()?.rootViewController = viewController

So replace these lines :

 UIApplication.shared.keyWindow?.rootViewController = viewController
                            self.dismiss(animated: true, completion: nil)

With :

GetWindow()?.rootViewController = viewController
GetWindow()?.makeKeyAndVisible()

And you don’t need to dismiss as it set a new root view controller with this code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading