“Type of expression is ambiguous without more context” error with GIDSignIn?

huangapple go评论54阅读模式
英文:

"Type of expression is ambiguous without more context" error with GIDSignIn?

问题

以下是您的代码的翻译部分:

我最近一直在构建一个应用程序,该应用程序通过Google Firebase使用Google Sign-In。我在修复此错误时遇到了麻烦。

这是我的代码:

import SwiftUI
import Firebase
import GoogleSignIn
import FirebaseAuth

struct LoginView: View {
    @State private var email = ""
    @State private var password = ""
    @State private var isShowingRegistrationView = false
    @EnvironmentObject var contentVM: ContentViewModel
    var body: some View {
        ZStack{
            Image("background1")
                .resizable()
                .ignoresSafeArea()
            VStack{
                VStack{
                    Text("登录")
                        .font(.system(size: 42, weight: .bold, design: .rounded))
                        .foregroundColor(.white)
                    Text("继续")
                        .font(.title3)
                        .foregroundColor(.white.opacity(0.7))
                }
                .padding(.vertical, 60)
                VStack{
                    TextField("输入电子邮件", text: $email)
                        .textInputAutocapitalization(.never)
                        .disableAutocorrection(true)
                        .modifier(CustomTextField())
                    SecureField("输入密码", text: $password)
                        .textInputAutocapitalization(.never)
                        .disableAutocorrection(true)
                        .modifier(CustomTextField())
                }
                .padding(.horizontal)
                .padding(.top, 60)
                Spacer()
                VStack(spacing: 20){
                    Button{
                        Task{
                            await contentVM.signIn(withEmail: email, password: password)
                        }
                    } label: {
                        Text("登录")
                    }
                    .buttonStyle(CustomButton())
                }
                HStack{
                    Text("还没有帐户?")
                        .font(.subheadline)
                        .foregroundColor(.white)
                    Button {
                        isShowingRegistrationView = true
                    } label: {
                        Text("注册")
                            .font(.subheadline.bold())
                            .foregroundColor(.green.opacity(0.7))
                    }
                }
                Spacer()
                Text("或")
                    .font(.system(size: 25, weight: .bold, design: .rounded))
                    .foregroundColor(.white)
                    .underline()
                GoogleSignInBtn {
                    guard let clientID = FirebaseApp.app()?.options.clientID else { return }

                    let config = GIDConfiguration(clientID: clientID)

                    //MARK: 错误出现在这里
                    GIDSignIn.sharedInstance.signIn(with: config, presenting: getRootViewController()) {  user, error in

                        if let error = error {
                            //...
                            return
                        }

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

                        let credential = GoogleAuthProvider.credential(withIDToken: idToken,
                                                                       accessToken: authentication.accessToken)
                        // 使用凭据进行Firebase身份验证

                    }

                } // GoogleSignInBtn
                Spacer()
            }
            .padding(.horizontal)
        }
        .sheet(isPresented: $isShowingRegistrationView) {
            RegistrationView()
        }
    }
}

希望这有助于您解决问题。如果您需要更多帮助,请告诉我。

英文:

I have recently been building an app that utilises Google Sign-In through Google Firebase. I am having trouble fixing this error.

This is my code:

import SwiftUI
import Firebase
import GoogleSignIn
import FirebaseAuth
struct LoginView: View {
@State private var email = ""
@State private var password = ""
@State private var isShowingRegistrationView = false
@EnvironmentObject var contentVM: ContentViewModel
var body: some View {
ZStack{
Image("background1")
.resizable()
.ignoresSafeArea()
VStack{
VStack{
Text("Log in")
.font(.system(size: 42, weight: .bold, design: .rounded))
.foregroundColor(.white)
Text("to continue")
.font(.title3)
.foregroundColor(.white.opacity(0.7))
}
.padding(.vertical, 60)
VStack{
TextField("Enter email", text: $email)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.modifier(CustomTextField())
SecureField("Enter password", text: $password)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.modifier(CustomTextField())
}
.padding(.horizontal)
.padding(.top, 60)
Spacer()
VStack(spacing: 20){
Button{
Task{
await contentVM.signIn(withEmail: email, password: password)
}
} label: {
Text("Log in")
}
.buttonStyle(CustomButton())
}
HStack{
Text("Don't have an account?")
.font(.subheadline)
.foregroundColor(.white)
Button {
isShowingRegistrationView = true
} label: {
Text("Sign up")
.font(.subheadline.bold())
.foregroundColor(.green.opacity(0.7))
}
}
Spacer()
Text("or")
.font(.system(size: 25, weight: .bold, design: .rounded))
.foregroundColor(.white)
.underline()
GoogleSiginBtn {
guard let clientID = FirebaseApp.app()?.options.clientID else { return }
let config = GIDConfiguration(clientID: clientID)
//MARK: Error occurs here
GIDSignIn.sharedInstance.signIn(with: config, presenting: getRootViewController()) {  user, error in
if let error = error {
//...
return
}
guard
let authentication = user?.authentication,
let idToken = authentication.idToken
else {
return
}
let credential = GoogleAuthProvider.credential(withIDToken: idToken,
accessToken: authentication.accessToken)
// Use the credential to authenticate with Firebase
}
} // GoogleSiginBtn
Spacer()
}
.padding(.horizontal)
}
.sheet(isPresented: $isShowingRegistrationView) {
RegistrationView()
}
}
}

I am following this YouTube Tutorial (https://www.youtube.com/watch?v=M5LiqOBDeGg), but not entirely - rather, I am taking relevant code and putting it into an existing app. The video creator solves this issue at around 14:15 in the tutorial, in case you want to go through it.

Does anyone know how to solve this issue? I am relatively new to Firebase code and am afraid to try changing too many things in case I break the project.

答案1

得分: 0

The issue is the code that is generating the error .signIn(with: config, presenting: ... is no longer the correct syntax. Looks like some updates were made on the google side of things.

Try replacing the block of code within the GoogleSiginBtn with the following:

guard let clientID = FirebaseApp.app()?.options.clientID else { return }

let config = GIDConfiguration(clientID: clientID)

GIDSignIn.sharedInstance.configuration = config

GIDSignIn.sharedInstance.signIn(withPresenting: view.getRootViewController()) { signResult, error in

    if let error = error {
       //...
       return
    }

     guard let user = signResult?.user,
           let idToken = user.idToken else { return }

     let accessToken = user.accessToken

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

    // Use the credential to authenticate with Firebase

}
英文:

The issue is the code that is generating the error .signIn(with: config, presenting: ... is no longer the correct syntax. Looks like some updates were made on the google side of things.

Try replacing the block of code within the GoogleSiginBtn with the following:

guard let clientID = FirebaseApp.app()?.options.clientID else { return }
let config = GIDConfiguration(clientID: clientID)
GIDSignIn.sharedInstance.configuration = config
GIDSignIn.sharedInstance.signIn(withPresenting: view.getRootViewController()) { signResult, error in
if let error = error {
//...
return
}
guard let user = signResult?.user,
let idToken = user.idToken else { return }
let accessToken = user.accessToken
let credential = GoogleAuthProvider.credential(withIDToken: idToken.tokenString, accessToken: accessToken.tokenString)
// Use the credential to authenticate with Firebase
}

huangapple
  • 本文由 发表于 2023年5月13日 11:42:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定