SwiftUI集成Google登录遇报错:FirebaseApp未找到等问题求助
Google登录集成错误修复指南
问题背景
已通过Package Dependencies安装Firebase和GoogleSignIn,但运行代码时触发三个编译错误。
代码片段
import SwiftUI import Firebase import GoogleSignIn import FirebaseAuth // … struct ContentView: View { // … private func signInWithGoogle() { // Line 40: guard let clientID = FirebaseApp.app()?.options.clientID else { print("Failed to get client ID") return } let config = GIDConfiguration(clientID: clientID) // Line 47: GIDSignIn.sharedInstance.signIn(with: config, presenting: getRootViewController()) { user, error in if let error = error { print("Error during sign-in: \(error.localizedDescription)") return } guard let authentication = user?.authentication, let idToken = authentication.idToken, let accessToken = authentication.accessToken else { print("Error retrieving tokens") return } let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken) Auth.auth().signIn(with: credential) { authResult, error in if let error = error { print("Error signing in with Google: \(error.localizedDescription)") } else { // Successfully signed in self.user = authResult?.user self.isSignedIn = true } } } } // … }
报错信息
/Users/ayaangrover/Documents/Github/waffle/Waffle/Waffle/ContentView.swift:40:30 在作用域中找不到‘FirebaseApp’
/Users/ayaangrover/Documents/Github/waffle/Waffle/Waffle/ContentView.swift:47:67 无法将‘UIViewController’类型的值转换为预期的参数类型‘String?’
/Users/ayaangrover/Documents/Github/waffle/Waffle/Waffle/ContentView.swift:47:92 调用中传入了多余的尾随闭包
分步修复方案
1. 修复“找不到FirebaseApp”错误
- 初始化Firebase:在App启动入口(如
App结构体的init方法或AppDelegate的启动回调)添加FirebaseApp.configure(),确保Firebase服务正确初始化。 - 补充模块导入:若使用Firebase模块化版本(v9+),需在文件顶部额外导入
FirebaseCore:import FirebaseCore - 检查依赖:确认Package Dependencies中已包含
FirebaseCore(模块化Firebase需单独引入该核心模块)。
2. 修复GIDSignIn方法调用错误(后两个报错)
GoogleSignIn 6.x+版本已更新API,旧版signIn(with:presenting:)方法已废弃,需替换为新版调用方式:
private func signInWithGoogle() { // 从GoogleService-Info.plist读取clientID(无需依赖Firebase) guard let clientID = GIDConfiguration.shared.clientID else { print("Failed to get client ID") return } let config = GIDConfiguration(clientID: clientID) // 新版signIn方法 GIDSignIn.sharedInstance.signIn(withPresenting: getRootViewController(), configuration: config) { signInResult, error in if let error = error { print("Error during sign-in: \(error.localizedDescription)") return } // 新版令牌获取方式 guard let user = signInResult?.user, let idToken = user.idToken?.tokenString, let accessToken = user.accessToken.tokenString else { print("Error retrieving tokens") return } let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken) Auth.auth().signIn(with: credential) { authResult, error in if let error = error { print("Error signing in with Google: \(error.localizedDescription)") } else { self.user = authResult?.user self.isSignedIn = true } } } }
核心变更点:
- 使用
signIn(withPresenting:configuration:completion:)替代旧方法,参数顺序和类型匹配新版API - 从
signInResult?.user中直接获取idToken和accessToken,替代旧版user?.authentication逻辑 - 可选优化:通过
GIDConfiguration.shared.clientID直接读取配置文件中的clientID,无需依赖FirebaseApp
3. 确保根视图控制器获取方法正确
如果getRootViewController()是自定义方法,需确保它能正确返回当前应用的根视图控制器,示例实现:
private func getRootViewController() -> UIViewController { guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, let rootVC = windowScene.windows.first?.rootViewController else { return UIViewController() } return rootVC }
内容的提问来源于stack exchange,提问作者Ayaan Grover
相关产品推荐
相关产品推荐

