-1

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

ERIC
  • 1
  • 3
  • Welcome to SO. Please check out [ask]. If your question is broad (like including 2 questions in one), it's likely to get closed as needing more focus. Your second question is a duplicate of this: https://stackoverflow.com/questions/57134259/how-to-resolve-keywindow-was-deprecated-in-ios-13-0 In terms of your first question, it's not clear what your issue is or what you're asking besides for "advice" – jnpdx Jul 13 '22 at 04:35
  • The first question wants to ask where my code is wrong and needs to be fixed because it doesn't work. For the second question I tried other people's answers, but still wrong, hope to get help, thank you very much – ERIC Jul 13 '22 at 04:41
  • windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead – ERIC Jul 13 '22 at 04:42
  • I have edited the question so it should be clearer, thanks for the reminder – ERIC Jul 13 '22 at 05:12

1 Answers1

0

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.

Vikas saini
  • 648
  • 1
  • 17