I am having issues implementing the google sign in process in my swiftUI file. I believe i’ve imported the correct pods and have been at this for hours trying to get this to work, but I keep getting these similar errors on line 142:
1: Cannot convert value of type ‘GIDConfiguration’ to expected argument type ‘UIViewController’
2: Cannot find ‘getRootViewController’ in scope
3: Extra trailing closure passed in call
4: Unable to infer type of a closure parameter ‘error’ in the current context
5: Unable to infer type of a closure parameter ‘user’ in the current context
I’ve been trying to troubleshoot this in various different ways. Not sure what the problem is, pods, SDK, or something with my delegate. Thanks for the help. For some reason I don’t see an Info.plist file on my left toolbar of this new project. (Photo attached below) I tried to create one and target it, but now none of my schemes are building :(( Any help would be greatly appreciated on what is going wrong here!
Here is my swiftUI code:
import SwiftUI
import Firebase
import AuthenticationServices
import UIKit
import GoogleSignIn
// Define the CustomTextFieldStyle here
struct CustomTextFieldStyle: TextFieldStyle {
var borderColor: Color
func _body(configuration: TextField<Self._Label>) -> some View {
configuration
.padding(10)
.background(RoundedRectangle(cornerRadius: 8).fill(Color.white))
.overlay(RoundedRectangle(cornerRadius: 8).stroke(borderColor, lineWidth: 1))
}
}
struct SignUp: View {
@State private var name: String = ""
@State private var email: String = ""
@State private var password: String = ""
@State private var confirmPassword: String = ""
private var isFormValid: Bool {
!name.isEmpty && !email.isEmpty && !password.isEmpty && !confirmPassword.isEmpty
}
private var confirmPasswordFieldColor: Color {
if confirmPassword.isEmpty {
return Color.gray
} else {
return password == confirmPassword ? Color.green : Color.red
}
}
private var passwordFieldColor: Color {
if !confirmPassword.isEmpty {
return password == confirmPassword ? Color.green : Color.gray
} else {
return Color.gray
}
}
var body: some View {
NavigationView {
VStack(spacing: 25) {
Spacer(minLength: 1)
TextField("Name *", text: $name)
.textFieldStyle(CustomTextFieldStyle(borderColor: Color.gray))
.padding(.horizontal)
TextField("Email *", text: $email)
.textFieldStyle(CustomTextFieldStyle(borderColor: Color.gray))
.padding(.horizontal)
SecureField("Password *", text: $password)
.textFieldStyle(CustomTextFieldStyle(borderColor: passwordFieldColor))
.padding(.horizontal)
SecureField("Confirm Password *", text: $confirmPassword)
.textFieldStyle(CustomTextFieldStyle(borderColor: confirmPasswordFieldColor))
.padding(.horizontal)
Button("Already have an account? Log In", action: switchToSignIn)
.padding(.bottom, 10)
.font(.footnote)
.foregroundColor(.blue)
Button("Sign Up", action: signUpTapped)
.disabled(!isFormValid || password != confirmPassword)
.frame(width: 200, height: 50)
.background(isFormValid && password == confirmPassword ? Color.blue : Color.gray)
.foregroundColor(.white)
.font(.headline)
.cornerRadius(15)
.overlay(
RoundedRectangle(cornerRadius: 15)
.stroke(Color.blue, lineWidth: 2)
)
.padding()
Button(action: signInWithGoogle) {
HStack {
Image("Google") // Replace with your image name
.resizable()
.scaledToFit()
.frame(width: 16, height: 16)
Text("Sign Up with Google")
}
.frame(width: 280, height: 45)
.background(Color.black)
.foregroundColor(.white)
.cornerRadius(10)
.padding()
}
SignInWithAppleButton(
onRequest: { request in
// Handle Apple Sign Up request if needed
},
onCompletion: { result in
// Handle Apple Sign Up completion if needed
}
)
.frame(width: 280, height: 45)
.padding(3)
Spacer()
}
.padding()
.navigationTitle("Sign Up")
}
}
func signUpTapped() {
// Check if the form is valid and passwords match
guard isFormValid, password == confirmPassword else { return }
// Create a new user with Firebase Authentication
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let error = error {
// Handle any errors here, such as displaying an alert
print("Error signing up: \(error.localizedDescription)")
} else {
// The user is signed up successfully
// Additional steps after successful signup, like navigating to another view
print("User signed up successfully")
}
}
}
func switchToSignIn() {
// Logic to switch to the Sign In view
}
func signInWithGoogle() {
guard let clientID = FirebaseApp.app()?.options.clientID else { return }
let config = GIDConfiguration(clientID: clientID)
GIDSignIn.sharedInstance.signIn(with: config, presenting: getRootViewController()) { [weak self] user, error in
if let error = error {
// Handle error
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) { authResult, error in
if let error = error {
// Handle error
print(error.localizedDescription)
return
}
// User is signed in
// You can post a notification or use a published property to update your UI
}
}
}
struct SignUpView_Previews: PreviewProvider {
static var previews: some View {
SignUp()
}
}
}




