如何从App Delegate切换到其他视图控制器? [英] How do I switch to a different view controller from the App Delegate?

查看:35
本文介绍了如何从App Delegate切换到其他视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户登录Google后,我想将其带到主屏幕;但是,代码无法完全执行此操作.

Once the user signs in with Google, I want to take them to the home screen; however, the code does not fully execute this.

这是代码:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
      if let error = error {
        if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
          print("The user has not signed in before or they have since signed out.")
        } else {
          print("\(error.localizedDescription)")
        }
        return
      }
        let firstName = user.profile.givenName
        let lastName = user.profile.familyName
        let email = user.profile.email
    //Firebase sign in
      guard let authentication = user.authentication else {return}
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)

        Auth.auth().signIn(with: credential) { (authResult, error) in
            if let error = error {
                print("Firebase sign In error")
                print(error)
                return
            } else {
                let db = Firestore.firestore()

                db.collection("users").addDocument(data: ["firstName": firstName!, "lastName": lastName!, "email": email!, "uid": authResult!.user.uid]) { (error) in

                    if error != nil {
                        print("Error: User data not saved")
                    }
            }
            print("User is signed in with Firebase")
            let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let homeViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeVC") as! HomeViewController
            self.window?.rootViewController = homeViewController
            self.window?.makeKeyAndVisible()

        }
    }
}

更具体地说:

Auth.auth().signIn(with: credential) { (authResult, error) in
            if let error = error {
                print("Firebase sign In error")
                print(error)
                return
            } else {
                let db = Firestore.firestore()

                db.collection("users").addDocument(data: ["firstName": firstName!, "lastName": lastName!, "email": email!, "uid": authResult!.user.uid]) { (error) in

                    if error != nil {
                        print("Error: User data not saved")
                    }
            }
            print("User is signed in with Firebase")
            let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let homeViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeVC") as! HomeViewController
            self.window?.rootViewController = homeViewController
            self.window?.makeKeyAndVisible()

        }
    }

print(用户已使用Firebase登录")确实发生了,但是它无法切换HomeViewController,所以我不确定在这里做错了什么.

the print("User is signed in with Firebase") does take place but it fails to switch the HomeViewController and I'm not sure what it is that I am doing wrong here.

推荐答案

我的猜测是窗口为nil,这就是为什么您的代码无法正确执行的原因.如果您的目标是iOS 13,请尝试以下代码.理想情况下,如果您的应用还支持iOS 12、11等,则希望将登录代码移至SceneDelegate内并在AppDelegate中进行复制.

My guess is that window is nil and that's why your code isn't executing properly. Try the code below if you are targeting iOS 13. Ideally, you would want to move the Sign-in code inside SceneDelegate and duplicate it in AppDelegate if your app also supports iOS 12, 11 etc.

   Auth.auth().signIn(with: credential) { (authResult, error) in
        if let error = error {
            print("Firebase sign In error")
            print(error)
            return
        } else {
            let db = Firestore.firestore()

            db.collection("users").addDocument(data: ["firstName": firstName!, "lastName": lastName!, "email": email!, "uid": authResult!.user.uid]) { (error) in

                if error != nil {
                    print("Error: User data not saved")
                }
            }
            print("User is signed in with Firebase")
            DispatchQueue.main.async {
                let scene = UIApplication.shared.connectedScenes.first
                if let sceneDelegate = scene?.delegate as? SceneDelegate {  
                    let window = sceneDelegate?.window
                    let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                    let homeViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeVC") as! HomeViewController
                    window?.rootViewController = homeViewController
                    window?.makeKeyAndVisible()

                }
            }
        }
    }

这篇关于如何从App Delegate切换到其他视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆