【问题标题】:Google and Facebook Firebase Auth using swift 3Google 和 Facebook Firebase Auth 使用 swift 3
【发布时间】:2017-07-26 12:21:31
【问题描述】:

当 facebook 提示时,我的 google 标志没有提示用户在他们的个人资料中允许权限。为什么?

我正在视图控制器中实现我的 google 登录,而不是在指南中的应用程序委托中。我不确定我所做的是否正确。我已经尝试了好几个小时了。

这是我的代码:

苹果委托.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FIRApp.configure()
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

        return true
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let handled =  FBSDKApplicationDelegate.sharedInstance().application(application,
                                                                     open: url,
                                                                     sourceApplication: sourceApplication,
                                                                     annotation: annotation)
        GIDSignIn.sharedInstance().handle(url,
                                          sourceApplication: sourceApplication,
                                          annotation: annotation)

        return handled
    }


    // my viewcontroller.swift

    class LoginViewController: UIViewController, FBSDKLoginButtonDelegate, NVActivityIndicatorViewable, GIDSignInUIDelegate, GIDSignInDelegate {

        @IBOutlet weak var fbLogin: FBSDKLoginButton!
        var databaseRef: FIRDatabaseReference!
        var size = CGSize(width: 30, height: 30)

        override func viewDidLoad() {
            super.viewDidLoad()

            GIDSignIn.sharedInstance().uiDelegate = self
            GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
            GIDSignIn.sharedInstance().delegate = self


            NVActivityIndicatorView.DEFAULT_TYPE = .ballTrianglePath
            NVActivityIndicatorView.DEFAULT_BLOCKER_DISPLAY_TIME_THRESHOLD = 2

            FIRAuth.auth()?.addStateDidChangeListener() { (auth, user) in
                if user != nil{
                    self.performSegue(withIdentifier: "HomeTabSegue", sender: self)
                    NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
                }
                else
                {
                    self.fbLogin.readPermissions = ["public_profile", "email"]
                    self.fbLogin.delegate = self
                    self.fbLogin.isHidden = false
                }
            }
        }

        func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
            if let error = error {
                print(error.localizedDescription)
                return
            }

            guard let authentication = user.authentication else { return }
            let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                              accessToken: authentication.accessToken)
            FIRAuth.auth()?.signIn(with: credential) { (user, error) in
                self.databaseRef = FIRDatabase.database().reference()
                self.databaseRef.child("Users").child(user!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
                    let snapshot = snapshot.value as? NSDictionary
                    if(snapshot == nil)
                    {
                        self.databaseRef.child("Users").child(user!.uid).setValue(["name" : user?.displayName, "email": user?.email])
                    }
                })

            }
        }
        func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
            if let error = error {
                print(error.localizedDescription)
                return
            }

            try! FIRAuth.auth()!.signOut()

        }


        func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
            print("user has log in fb")

            let activityData = ActivityData()

            NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)

            self.fbLogin.isHidden = true

            if (error != nil)
            {
                NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
                print(error.localizedDescription)
                self.fbLogin.isHidden = false
            }
            else if(result.isCancelled)
            {
                print("cancelled is pressed")
                NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
                self.fbLogin.isHidden = false
            }
            else
            {
                let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
                FIRAuth.auth()?.signIn(with: credential) { (user, error) in
                    print("user has log into firebase")
                self.databaseRef = FIRDatabase.database().reference()
                self.databaseRef.child("Users").child(user!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
                    let snapshot = snapshot.value as? NSDictionary
                    if(snapshot == nil)
                        {
                            self.databaseRef.child("Users").child(user!.uid).setValue(["name" : user?.displayName, "email": user?.email])
                        }
                    })
                }
            }
        }

        func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
            print("user has log out")
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }

到目前为止,我的代码可以在 facebook 和 google 中将用户保存到 firebase。只是当 google 签名时,它不会在登录 facebook 时征求我的许可。请给我一个正确的方法如何登录多个身份验证或一个好的项目示例会很好,因为我没有看到一个项目只是实现这些。请给我一个想法。

【问题讨论】:

    标签: ios facebook firebase swift3 firebase-authentication


    【解决方案1】:

    我认为在 application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool 中,需要得到 GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation) 并将其与 FB 一起返回。所以应该是这样的:

    return handled || GIDSignIn.sharedInstance().handle(url,
                                          sourceApplication: sourceApplication,
                                          annotation: annotation)
    

    因为根据文档here

    如果 GIDSignIn 处理了这个 URL,则返回 YES。

    【讨论】:

    • 是的,试过了,但为什么每次登录都没有征求我的同意?它只会直接进入我的主屏幕。
    • 啊我知道因为它在视图中确实加载了。知道如何将其添加到 viewdid 出现了吗?
    • 对不起,我不明白这个问题。您介意详细说明吗?
    【解决方案2】:

    我猜你的问题是没有在你的 loginViewController 中调用 GIDSignIn 登录函数:

    @IBAction func signInButtonDidTouch(_ sender: Any) {
    
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().delegate = self
    
        if GIDSignIn.sharedInstance().hasAuthInKeychain() {
            GIDSignIn.sharedInstance().signInSilently()
        } else {
            GIDSignIn.sharedInstance().signIn()
        }
    }
    

    上面的代码我有自定义GG登录按钮@IBOutlet weak var signInButton: GIDSignInButton!

    【讨论】:

      猜你喜欢
      • 2021-07-13
      • 1970-01-01
      • 2017-03-10
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 2017-03-26
      • 2021-08-19
      相关资源
      最近更新 更多