我试图创建一个谷歌登录页面,我有一个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
}
}
}
}
字符串
1条答案
按热度按时间1cklez4t1#
述的方法
字符串
已经更新到
型
而配置部分将在plist中处理。
1.那样使用它
型
1.在plist中添加客户端ID。
型
**注意:-**返回
GIDSignInResult
而不是GIDGoogleUser