我正在尝试创建一个谷歌登录页面,我有一个signInModel,它有一些错误,我需要帮助解决 错误就在眼前
GIDSignIn.sharedInstance.signIn(with: configuration, presenting: rootViewController) { \[unowned self\] user, error in
错误是无法将类型“GIDConfiguration”的值转换为预期的参数类型“UIViewController”,无法将类型“UIViewController”的值转换为预期的参数类型“String?”,在调用中传递了额外的尾随闭包
和线
guard let authentication = user?.authentication, let idToken = authentication.idToken else { return }
错误是“GIDGoogleUser”类型的值没有成员“身份验证”
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
}
}
}
}
方法
GIDSignIn.sharedInstance.signIn(with: configuration, presenting: rootViewController)
已更新为
signIn(withPresenting: UIViewController)
和配置部分将在plist中处理。
就这样用吧
GIDSignIn.sharedInstance.signIn(withPresenting: rootViewController) { [unowned self] result, error in authenticateUser(for: result.user, with: error) }
注意:-它返回
GIDSignInResult
而不是GIDGoogleUser