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?
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.