【问题标题】:Gmail authentication not returning to applicationGmail 身份验证未返回到应用程序
【发布时间】:2017-05-24 01:30:05
【问题描述】:

我已经使用 Firebase 实现了 gmail 登录,并且在按下连接了操作 GIDSignIn.sharedInstance().signIn() 的自定义按钮后,我成功地让用户验证了他的电子邮件。

但是,一旦用户允许基本的个人资料访问,它就不会返回到应用程序,而只是转到此屏幕:

有谁知道在用户认证后如何让它返回应用程序?这是我的控制台:

017-01-09 09:47:46.565367 Dog_Log[3922:829530] [MC] Reading from private effective user settings.
2017-01-09 09:47:53.702 Dog_Log[3922:] <FIRAnalytics/WARNING> Implementation of application:openURL:sourceApplication:annotation: not found. Please add the handler into your App Delegate. Class: Dog_Log.AppDelegate

【问题讨论】:

    标签: ios swift firebase swift3 firebase-authentication


    【解决方案1】:

    问题:

    警告告诉你找不到application(_:open:options:)的实现:

    2017-01-09 09:47:53.702 Dog_Log[3922:] <FIRAnalytics/WARNING> Implementation of application:openURL:sourceApplication:annotation: not found. Please add the handler into your App Delegate. Class: Dog_Log.AppDelegate
    

    解决方案:

    您可能忘记了要实现的application(_:open:options:) 方法,其中调用了handle(_:sourceApplication:annotation:),它处理重定向到您的应用程序。您的AppDelegate.swift 应该如下所示(提示在代码 cmets 中):

    import UIKit
    import Firebase
    import GoogleSignIn
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
      var window: UIWindow?
    
    
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        // configure firebase
        FIRApp.configure()
    
        // configure google
        GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
    
        return true
      }
    
      func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    
        // handled to go back to application after google log in
        let handled = GIDSignIn.sharedInstance().handle(url, sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: [:])
    
        return handled
      }
    
      // other methods
    }
    

    您制作的示例 ViewController 符合 GIDSignInUIDelegate 和 GIDSignInDelegate,以访问 signIn 方法并将其与自定义按钮的 @IBAction 一起使用:

    import UIKit
    import Firebase
    import GoogleSignIn
    
    
    class ViewController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        // make the view controller the delegate
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().delegate = self
      }
    
      // your action of the button to call the signIn method
      @IBAction func pressGoogleSignInButton(_ sender: UIButton) {
        GIDSignIn.sharedInstance().signIn()
      }
    
      func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
          print("failed to log into google: ", error)
          return
        }
    
        print("successfully logged into google ", user)
    
        guard let idToken = user.authentication.idToken else { return }
        guard let accessToken = user.authentication.accessToken else { return }
        let credentials = FIRGoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
    
        FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
          if let error = error {
            print("Faild to create a firebase user with google account: ", error)
            return
          }
    
          guard let uid = user?.uid else { return }
          print("Successfully logged into firebase with google ", uid)
          // redirect to the next screen after successful login
    
        })
      }
    }
    

    【讨论】:

    • 所以如果这应该是我的 AppDelegate.swift,对于 ViewController,我应该只有调用 GIDSignIn.sharedInstance().signIn() 的 IBAction 吗?
    • @MarksCode 更新了我的答案,因此您可以在特定视图控制器中按钮的 IBAction 中使用该方法
    • 是的,很高兴能帮到你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2013-03-08
    • 2023-04-07
    • 2020-12-17
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多