使用 Google 身份验证登录

Sign In with Google Authentication

我已经在我的应用程序中通过 Firebase 实现了 Google 身份验证。除了一个我似乎找不到的小问题外,一切都很顺利。每当用户打开提示他们“使用 Google 登录”的页面(即登录页面或注册页面)时,横幅会在消失前短暂出现。我根本不希望它出现,除非用户单击“使用 Google 登录”按钮。我怎样才能摆脱这个?

WelcomeViewController(具有 google 登录名的视图控制器)

import UIKit
import FirebaseAuth
import Firebase
import FBSDKLoginKit
import GoogleSignIn
 
class WelcomeViewController: UIViewController, GIDSignInDelegate {
    
    @IBOutlet weak var stackView: UIStackView!
    @IBOutlet weak var signInFacebookButton: UIButton!
    @IBOutlet weak var signInGoogleButton: UIButton!
     
    override func viewDidLoad() {
        super.viewDidLoad()

        setUpGoogleButton()
            
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance().signIn()
    }
    
// SIGN IN WITH GOOGLE
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let err = error {
            print("Failed to log into Google: ", err)
            return
        }
        print("Successfully logged into Google")
        
        guard let authentication = user.authentication else { return }
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
        
        Auth.auth().signIn(with: credential, completion: { (user, error) in
            if let err = error {
                print("Failed to create a Firebase User with Google account: ", err)
                return
            }
            // Successfully logged in
            guard let uid = user?.user.uid else { return }
            print("Successfully logged into Firebase with Google", uid)
            // switch to tab bar controller
            let tabBarC = self.storyboard?.instantiateViewController(withIdentifier: "mainTabBarController") as! TabBarController
            tabBarC.modalPresentationStyle = .fullScreen
            self.present(tabBarC, animated: true, completion: nil)
            print("Switched to TabBarController")
        })
    }
    
    fileprivate func setUpGoogleButton() {
        let button = signInGoogleButton
        button?.layer.borderWidth = 0
        button?.backgroundColor = UIColor.init(red: 130/255, green: 178/255, blue: 189/255, alpha: 1)
        button?.layer.cornerRadius = 20.0
        button?.tintColor = UIColor.white
        button!.addTarget(self, action:
                            #selector(handleCustomGoogleSignIn), for: .touchUpInside)
        
        GIDSignIn.sharedInstance()?.delegate = self
        }
    
    @objc func handleCustomGoogleSignIn() {
        GIDSignIn.sharedInstance().signIn()
        }

我已将 link 附加到发生的屏幕录像中。录屏中显示的第二个页面和下面的代码完全一样,所以也有同样的问题。感谢任何帮助,谢谢!

https://drive.google.com/file/d/1t4KV0Z6qwfCK56Gf2314wXWhAeQR0wUs/view?usp=sharing

那是因为您的代码在 viewDidLoad() 中。您正在实施此方法:

GIDSignIn.sharedInstance().signIn()

这会在视图加载后立即触发登录方法(因为您在 viewDidLoad() 中实现它),这会导致瞬间登录弹出窗口消失.

您不应在此处实施该方法,而应仅在 handleCustomGoogleSignIn() 内实施。

结论,你的 viewDidLoad() 应该是这样的:

override func viewDidLoad() {
        super.viewDidLoad()

        setUpGoogleButton()
            
        GIDSignIn.sharedInstance()?.presentingViewController = self
    }