GIDSignIn Type of expression is ambiguous without more context

1.6k Views Asked by At

I've been following this tutorial here for adding google sign in to my iOS app:https://www.youtube.com/watch?v=TfYIH_23c6w&t=353s

and everything looks good except for this line here:

GIDSignIn.sharedInstance.signIn(with: config, presenting:ApplicationUtility.rootViewController) {

The error is Type of expression is ambiguous without more context

in this file:

//
//  LoginVM.swift
//  frcscout
//
//  Created by Elliot Scher on 12/14/22.
//

import Foundation
import Firebase
import GoogleSignIn

class SignUpViewModel: ObservableObject {
    
    @Published var isLogin: Bool = false
    
    func signInWithGoogle() {
                
        guard let clientId = FirebaseApp.app()?.options.clientID else { return }
        
        let config = GIDConfiguration(clientID: clientId)
        
        GIDSignIn.sharedInstance.signIn(with: config, presenting:ApplicationUtility.rootViewController) {
            [self] user, err in
            
            if let error = err {
                print(error.localizedDescription)
                return
            }
            
            guard
                let authentication = user.authentication,
                let idToken = authentication.idToken
            else { return }
            
            let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: authentication.accessToken)
            
            Auth.auth().signIn(with: credential) { result, error in
                if let error = err {
                    print(error.localizedDescription)
                    return
                }
                
                guard let user = result?.user else { return }
                print(user.displayName)
                isLogin.toggle()
            }
                    
        }
        
    }
}

I'm not sure what this error means. How can I fix it? Thanks!!

2

There are 2 best solutions below

0
Trương Quang ân On BEST ANSWER

1/ Config info.plist

<key>GIDClientID</key>
<string>YOUR_IOS_CLIENT_ID</string>
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
    </array>
  </dict>
</array>

https://developers.google.com/identity/sign-in/ios/start-integrating#add_client_id

2/ Download new GoogleService-Info.plist from firebase

3/ use below function

@objc func signInWithGooglePressed(sender: Any) {

    // Start the sign in flow!
    GIDSignIn.sharedInstance.signIn(withPresenting: self) { signInResult, err in

        if let error = err {
            print(error)
            return
        }

        guard
            let authentication = signInResult?.user,
            let idToken = authentication.idToken?.tokenString
        else {
            return
        }
        
        let user = signInResult?.user

        let emailAddress = user?.profile?.email

        let fullName = user?.profile?.name
        let givenName = user?.profile?.givenName
        let familyName = user?.profile?.familyName

        let credential = GoogleAuthProvider.credential(withIDToken: idToken,
                                                     accessToken: authentication.accessToken.tokenString)

        FirebaseProvider.Instance.firebaseGoogleLogin(credential: credential)
    }
    
    
}
0
Omar Elsaeed On

-All i need to do is not accessing the user directly, instead i access the user from the signInResult

here is the new code

GIDSignIn.sharedInstance.signIn(withPresenting: self) { signInResult, error in
    guard error == nil else { return }
    guard let signInResult = signInResult else { return }

    let user = signInResult.user // here i got access to the user

    let emailAddress = user.profile?.email

    let fullName = user.profile?.name
    let givenName = user.profile?.givenName
    let familyName = user.profile?.familyName

    let profilePicUrl = user.profile?.imageURL(withDimension: 320)
}