I want to achieve firebase key: Safety-Check,
value: "admin" into view "adminVC", Value: "ON" into view "MainTabBarController",
Without this key: Safety-Check into view "SignUpViewControllerID"
Below is my current code but it doesn't work, hope someone can give me some advice, thanks
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") {
GetWindow()?.rootViewController = viewController
GetWindow()?.makeKeyAndVisible()
}
if cheak == "ON" {
if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "MainTabBarController") {
GetWindow()?.rootViewController = viewController
GetWindow()?.makeKeyAndVisible()
}else{
if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SignUpViewControllerID") {
GetWindow()?.rootViewController = viewController
GetWindow()?.makeKeyAndVisible()
[solved]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
CodePudding user response:
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.