调用GoogleSignIn().signIn()导致iPhone应用崩溃求助
Flutter iOS端GoogleSignIn().signIn()崩溃排查与解决方法
问题概述
调用GoogleSignIn().signIn()时,iOS模拟器直接崩溃,丢失设备连接,日志提示无法终止应用进程、Dart编译器异常退出;Android端完全正常,iOS端此前运行正常,后续突然失效。
排查与解决步骤
1. 清理模拟器缓存与项目依赖
- 彻底关闭iOS模拟器、Xcode及Flutter运行终端
- 执行以下命令清理项目缓存与iOS依赖:
flutter clean rm -rf ios/Pods rm ios/Podfile.lock pod install --repo-update - 重启模拟器后重新运行项目
2. 验证iOS端Google登录配置
- 检查
ios/Runner/Info.plist,确保包含正确的URL Scheme与客户端ID配置:<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>com.googleusercontent.apps.你的客户端ID</string> </array> </dict> </array> <key>GIDClientID</key> <string>你的客户端ID.apps.googleusercontent.com</string> - 确认Google Cloud控制台中,iOS客户端ID的Bundle ID与项目
CFBundleIdentifier完全一致 - 重新下载
GoogleService-Info.plist替换ios/Runner目录下的旧文件,确保文件未损坏
3. 调整插件版本避免冲突
- 检查
pubspec.yaml中google_sign_in及相关依赖(如firebase_auth)的版本,尝试回退至之前可用的稳定版或更新至最新稳定版:dependencies: google_sign_in: ^5.4.0 firebase_auth: ^3.6.0 # 若集成Firebase则需配置 - 执行
flutter pub get后重新构建项目
4. 排查模拟器环境问题
- 切换至不同iOS版本的模拟器测试(如从iOS 16切换到iOS 15),排除系统兼容性问题
- 重置模拟器:打开模拟器 → 菜单栏
Device→Erase All Content and Settings...,重置后重新安装应用 - 确认模拟器网络正常,能访问Google服务
5. 修复Flutter环境问题
- 升级Flutter至最新稳定版:
flutter upgrade - 执行
flutter doctor检查环境,根据提示修复Xcode命令行工具、证书等问题
代码优化建议
显式初始化GoogleSignIn并指定客户端ID与权限范围,避免默认配置潜在问题:
final GoogleSignIn _googleSignIn = GoogleSignIn( scopes: ['email'], clientId: '你的iOS客户端ID.apps.googleusercontent.com', ); Future<void> signInWithGoogle(context) async { try { print("tried to googlesign in"); final GoogleSignInAccount? googleUser = await _googleSignIn.signIn(); print("passed it!"); if (googleUser == null) return; final GoogleSignInAuthentication googleAuth = await googleUser.authentication; final AuthCredential credential = GoogleAuthProvider.credential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, ); await _signInWithCredential(credential); } catch (e) { print(e); showModalBottomSheet( context: context, builder: (builder) { return Container( child: Text(e.toString()), ); }, ); } }
内容的提问来源于stack exchange,提问作者Sohatzky
相关产品推荐
相关产品推荐

