Google 身份验证 SWIFTUI:无法将类型为“GIDConfiguration”的值转换为预期的参数类型“UIViewController”

Google Auth SWIFTUI: Cannot convert value of type 'GIDConfiguration' to expected argument type 'UIViewController'

提问人:dk19 提问时间:11/16/2023 最后编辑:dk19 更新时间:11/16/2023 访问量:26

问:

我在 swiftUI 文件中实现 google 登录过程时遇到问题。我相信我已经导入了正确的 pod,并且已经花了几个小时试图让它工作,但我在第 142 行不断收到这些类似的错误:

1:无法将类型为“GIDConfiguration”的值转换为预期的参数类型“UIViewController” 2:在作用域中找不到“getRootViewController” 3:在调用中传递了额外的尾随闭包 4:无法推断当前上下文中闭包参数“错误”的类型 5:无法推断当前上下文中闭包参数“user”的类型

我一直在尝试以各种不同的方式解决这个问题。不确定问题出在哪里,pods,SDK或我的委托。感谢您的帮助。出于某种原因,我在这个新项目的左侧工具栏上看不到 Info.plist 文件。(下图附上)我试图创建一个并瞄准它,但现在我的计划都没有建立:(( 对于这里出了什么问题,任何帮助将不胜感激!

没有 Info.plist

这是我的swiftUI代码:

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()
        }
    }
}

iOS SwiftUI Firebase 身份验证 google-oauth

评论


答: 暂无答案