firebase 错误:无法将类型“GIDConfiguration”的值转换为预期的参数类型“UIViewController”

9rnv2umw  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(76)

我试图创建一个谷歌登录页面,我有一个signInModel,有几个错误,我需要帮助解决的错误是行GIDSignIn.sharedInstance.signIn(with: configuration, presenting: rootViewController) { \[unowned self\] user, error in的错误是无法转换类型的值“GIDConfiguration”到预期的参数类型“UIViewController”,无法转换类型的值“UIViewController”到预期的参数类型“字符串?”,额外的尾随闭包传递调用
guard let authentication = user?.authentication, let idToken = authentication.idToken else { return }错误是类型“GIDGoogleUser”的值没有成员“authentication”

import SwiftUI
import Firebase
import GoogleSignIn
import FirebaseAuth

class SignInViewModel: ObservableObject {
    // Part one
    enum signInState {
        case signedIn
        case signedOut
    }

    @Published var state: signInState = .signedOut

    func signIn() {
        if GIDSignIn.sharedInstance.hasPreviousSignIn() {
            GIDSignIn.sharedInstance.restorePreviousSignIn { [unowned self] user, error in
                authenticateUser(for: user, with: error)
            }
        } else {
            guard let clientID = FirebaseApp.app()?.options.clientID else { return }

            let configuration = GIDConfiguration(clientID: clientID)

            guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return }
            guard let rootViewController = windowScene.windows.first?.rootViewController else { return }

            GIDSignIn.sharedInstance.signIn(with: configuration, presenting: rootViewController) { [unowned self] user, error in
                authenticateUser(for: user, with: error)
            }
        }
    }

    private func authenticateUser(for user: GIDGoogleUser?, with error: Error?) {
        if let error = error {
            print(error.localizedDescription)
            return
        }

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

        let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: user?.authentication?.accessToken ?? "")

        Auth.auth().signIn(with: credential) { [unowned self] (_, error) in
            if let error = error {
            print(error.localizedDescription)
            } else {
                self.state = .signedIn
            }
        }
    }
}

字符串

1cklez4t

1cklez4t1#

述的方法

GIDSignIn.sharedInstance.signIn(with: configuration, presenting: rootViewController)

字符串
已经更新到

signIn(withPresenting: UIViewController)


而配置部分将在plist中处理。
1.那样使用它

GIDSignIn.sharedInstance.signIn(withPresenting: rootViewController) { [unowned self] result, error in
                authenticateUser(for: result.user, with: error)
            }


1.在plist中添加客户端ID。

<key>GIDClientID</key>
<string>CLIENTID</string>

**注意:-**返回GIDSignInResult而不是GIDGoogleUser

相关问题