Ios 如何直接从Google登录web链接切换到另一个视图控制器?
我有一个带有谷歌登录按钮的登录屏幕。我可以点击按钮,然后按照链接使用我的谷歌帐户登录,但随后应用程序返回到登录屏幕,然后切换到我的应用程序的主屏幕。如何解决此问题 以下是我在AppDelegate屏幕上的代码:Ios 如何直接从Google登录web链接切换到另一个视图控制器?,ios,swift,google-signin,google-authentication,Ios,Swift,Google Signin,Google Authentication,我有一个带有谷歌登录按钮的登录屏幕。我可以点击按钮,然后按照链接使用我的谷歌帐户登录,但随后应用程序返回到登录屏幕,然后切换到我的应用程序的主屏幕。如何解决此问题 以下是我在AppDelegate屏幕上的代码: class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate { var window: UIWindow? func application(_ application: UIA
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
GIDSignIn.sharedInstance()?.clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance()?.delegate = self
return true
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
return
}
guard let googleAuth = user.authentication else { return }
let googleCredential = GoogleAuthProvider.credential(withIDToken: googleAuth.idToken, accessToken:googleAuth.accessToken)
Auth.auth().signIn(with: googleCredential) { (result, error) in
if let error = error {
return
}
// Code to post user data to Firebase
}
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url)
}
以下是我在登录屏幕上的代码:
class LogIn_VC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Listens for changes in user auth status
authHandler = firebaseAuth.addStateDidChangeListener({ (auth, user) in
DispatchQueue.main.async {
if self.firebaseAuth.currentUser != nil {
// Set new rootVC as MainVC (when user logs in)
let mainVC = storyboard?.instantiateViewController(identifier: "MainVC") as? Main_VC
view.window?.rootViewController = mainVC
view.window?.makeKeyAndVisible()
}
}
})
}
// Login with google button
@IBAction func logInWithGoogle(_ sender: Any) {
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance()?.signIn()
}
}
事情不是这样的。 我不确定firebase和登录是否成功,但如果成功完成处理程序,您应该尝试以下操作:
let mainVC = storyboard?.instantiateViewController(identifier: "MainVC") as? Main_VC
let nav = UINavigationController(root: mainVC)
self.present(nav, animated: true, completion: nil)
检查新根目录的显示样式,您不希望用户只返回登录页面