如何从App Delegate切换到其他视图控制器?

时间:2020-06-13 00:07:27

标签: ios swift firebase firebase-authentication google-signin

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

这是代码:

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("User is signed in with Firebase")确实发生了,但是它无法切换HomeViewController,所以我不确定在这里做错了什么。

2 个答案:

答案 0 :(得分:0)

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

<div class="some_class">some_class 1</div>
<div class="some_class">some_class 2</div>

答案 1 :(得分:0)

代码中的问题是Firebase是异步的。所以这部分

} 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")

此打印声明

print("User is signed in with Firebase")

将在闭包内的代码之前执行。原因是代码比Internet快-您必须“等待”从Firebase返回的数据,然后再继续执行代码。要解决此问题,请将语句移动到闭包内部,以便它们在Firebase返回时执行

} 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")
         return
      }

      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()
    }

请注意,使用此代码流,每当现有用户对其进行签名时,它将在users集合中创建一个新文档。这可能不是目的。