You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter iOS端谷歌登录崩溃求助(Android端运行正常)

Flutter iOS谷歌登录崩溃排查与解决

Android平台谷歌登录流程正常,但iOS构建后点击谷歌登录按钮直接崩溃,已重新连接Firebase仍无效,结合提供的代码,可从以下方向排查修复:

1. 检查Info.plist核心配置

iOS谷歌登录必须配置正确的URL Scheme和查询白名单,否则会触发崩溃或无法唤起登录页:
打开ios/Runner/Info.plist,确认添加以下配置:

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <!-- 替换为GoogleService-Info.plist中的REVERSED_CLIENT_ID值 -->
      <string>com.googleusercontent.apps.xxxxxxxxx-xxxxxxxxxxxxxxxx</string>
    </array>
  </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>googleauth</string>
</array>

同时确保GoogleService-Info.plist已通过Xcode添加到Runner目标(勾选"Copy items if needed",避免仅放在文件夹中)。

2. 显式指定iOS Client ID初始化GoogleSignIn

Android可自动获取Client ID,但iOS必须显式指定,否则会导致登录流程异常:
修改authentication_repository.dart的signInWithGoogle方法:

Future<UserCredential?> signInWithGoogle() async {
  try {
    // 从GoogleService-Info.plist的CLIENT_ID字段获取iOS客户端ID
    final GoogleSignIn googleSignIn = GoogleSignIn(
      clientId: '你的iOS客户端ID',
    );
    final GoogleSignInAccount? userAccount = await googleSignIn.signIn();

    // 用户取消登录时直接返回,避免空值后续操作
    if (userAccount == null) {
      return null;
    }

    final GoogleSignInAuthentication googleAuth = await userAccount.authentication;
    // 确保认证信息非空
    if (googleAuth.accessToken == null || googleAuth.idToken == null) {
      throw Exception("无法获取谷歌认证信息");
    }

    final credentials = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    return await _auth.signInWithCredential(credentials);
  } on FirebaseAuthException catch (e) {
    throw NFirebaseAuthException(e.code).message;
  } on FirebaseException catch (e) {
    throw NFirebaseException(e.code).message;
  } on FormatException catch (_) {
    throw const NFormatException();
  } on PlatformException catch (e) {
    throw NPlatformException(e.code).message;
  } catch (e) {
    if (kDebugMode) print('Something went wrong: $e');
    return null;
  }
}

3. 校验Xcode项目基础配置

  • 确认iOS项目的Bundle ID与Firebase控制台注册的iOS应用Bundle ID完全一致(大小写敏感)。
  • 检查Runner项目的Signing & Capabilities,确保使用有效的开发者账号签名,避免真机上因签名问题崩溃。
  • 将Deployment Target设置为iOS 12.0及以上(谷歌登录最低支持版本)。

4. 清理缓存后重新构建

执行以下步骤彻底清理并重建项目:

  1. 运行flutter clean清理Flutter缓存
  2. 进入iOS目录:cd ios
  3. 删除Pods文件夹和Podfile.lock
  4. 重新安装依赖:pod install
  5. 重启Xcode和Flutter项目,重新构建iOS应用

内容的提问来源于stack exchange,提问作者Safwan Norasidin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 14:13:14