Google Sign-In via Firebase: GIDSignInDelegate does not conform to ViewController

怎甘沉沦 提交于 2019-12-07 06:11:09

问题


I'm introducing Google Sign-In to my app and whilst both Google and Firebase documentation is thorough enough, what I have done as they suggested is not sufficient... I am still getting this error. Hopefully this will help others with finding a solution to the problem when implementing their SDK....thanks in advance for reviewing this chunky one:

Here's the Firebase guide and the Google guide:

So

  1. Added Google to podfile - CHECK
  2. Added line into Bridging-Header - CHECK
  3. Added GoogleService-Info.plist & bundle identifier & reversed client ID to URL schemes - CHECK
  4. App Delegate has the following, with no errors but I notice there are likely to be conflicts between Facebook login (working correctly) and newly Google, which I've no idea how to handle together:

    P.S. I have NOT added GIDSignInDelegate to AppDelegate here because I am planning for my VC to handle the login logic, as you will see below...

  5. LoginVC ViewController code here:

    class LoginVC: UIViewController, UIViewControllerTransitioningDelegate, UITextViewDelegate, UITextFieldDelegate, GIDSignInDelegate, GIDSignInUIDelegate {
    
        override func viewDidLoad() {
        super.viewDidLoad()
        let ref = Firebase(url: "https://MYAPPID.firebaseio.com")
        GIDDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().signInSilently()  // for if the user has recently been authenticated
        }
    

Then this, which from what I can see... should be everything Google needs to talk to Firebase:

        // Implementing the required GIDSignInDelegate methods
        func googleSignIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
            if (error == nil) {
                // Auth with Firebase
                let userId = user.userID
                let idToken = user.authentication.idToken
                let fullName = user.profile.name
                let givenName = user.profile.givenName
                let familyName = user.profile.familyName
                let email = user.profile.email
                ref.authWithOAuthProvider("google", token: user.authentication.accessToken, withCompletionBlock: { (error, authData) in
                    // User is logged in!
                })
            } else {
                print("\(error.localizedDescription)")
            }
        }


        func googleSignOut() {
            GIDSignIn.sharedInstance().signOut()
            ref.unauth()
        }

        // Implement the required GIDSignInDelegate methods and Unauth when disconnected from Google
        func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!, withError error: NSError!) {
            ref.unauth()
        }


        // IBAction to handle the sign-in process
        @IBAction func googleButtonPressed(sender: TKTransitionSubmitButton!) {
            GIDSignIn.sharedInstance().signIn()
        }

Baffled? Sorry for the long one guys...but I've done everything the Firebase guide suggests and that means the logic in the Google doc for AppDelegate is all there in the ProfileVC. Any pointers?


回答1:


It's saying your class have not implemented required method for your GIDSignInDelegate. There are major change in name of method in Swift 3. So your new method will be

public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: NSError!)

Please check Library screen shot. So In is missing in new swift 3 convention of naming of methods or classes.




回答2:


I found needed methods by typing "func s" and searching in suggestions box

Oh, I miss Alt+Enter feature in Android Studio

(BTW this is for Swift 3 in XCode 8 beta)

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: NSError!) {
        let authentication = user.authentication
        let credential = FIRGoogleAuthProvider.credential(withIDToken: (authentication?.idToken)!,
                                                          accessToken: (authentication?.accessToken)!)
        let comp:FIRAuthResultCallback = { (user:FIRUser?, error:NSError?) in
            if error == nil {
                DataStorage.inst.user.id = user?.uid
            }
        }
        FIRAuth.auth()?.signIn(with: credential, completion: comp)
    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: NSError!) {

    }



回答3:


Just replace

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
withError error: NSError!) {
    if (error == nil) {
        // Perform any operations on signed in user here.

    } else {
        print("\(error.localizedDescription)")
    }}

with

public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    //Code
}



回答4:


Just add

#import "GoogleSignIn/GoogleSignIn.h"

to your Bridging header file and hit shift + cmd + k to clean.




回答5:


Go to: Link Binary with Libraries. Then, Click on Add, then Add Other. Press "Cmd + shift + G". Then, type: "/usr/lib". Then, Click on "libz.1.dylib". Press Ok to add and the errors will go away. Also, you are probably not completing all the functions that come along with this protocol. You will have to add following:

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
    withError error: NSError!) {
        if (error == nil) {
            // Perform any operations on signed in user here.

        } else {
            print("\(error.localizedDescription)")
        }
}

func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
    withError error: NSError!) {
        // Perform any operations when the user disconnects from app here.
        // ...
}

Also, make sure you have added the foll. line in Bridging Header.

#import <GoogleSignIn/GoogleSignIn.h>

UPDATE: GIDSignInDelegate can't be added to View Controller. Instead, you should add "GIDSignInUIDelegate" in VC and try to perform other(Gidsigndwlwgate) operations in App Delegate. It will work.



来源:https://stackoverflow.com/questions/36296798/google-sign-in-via-firebase-gidsignindelegate-does-not-conform-to-viewcontrolle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!