跳转到正文

iOS 上的 Firebase Google Login

GitHub

本指南将帮助你在 iOS 上通过 Firebase Authentication 集成 Google Sign-In。假设你已经完成了 Firebase Google 通用配置.

  1. 前往您的项目概览页面 console.cloud.google.com

    Firebase 项目概览
  2. 点击 Add app 按钮

    Firebase 添加应用按钮 Firebase 添加应用按钮
  3. 选择 iOS

    Firebase 添加 iOS 应用按钮
  4. 填写表单的第一部分

    1. 填写 Apple bundle ID
      1. 使用以下方式打开应用的 Xcode npx cap open ios
      2. 双击 App Xcode 项目导航器中的 App target
      3. 确保你处于 Targets -> App Xcode 中已选中 App 的 Targets 部分
      4. 找到你的 Bundle Identifier Xcode 项目设置中的 Bundle Identifier 字段
      5. 复制 Bundle Identifier 并将其粘贴到 Firebase 控制台 Firebase 添加应用 iOS Bundle ID 字段
    2. 点击 Register app 按钮 Firebase 添加应用 iOS 注册按钮
  5. 跳过此 Download config file 步骤

    Firebase 添加应用 iOS 跳过下载按钮
  6. 跳过此 Add firebase SDK 步骤

    Firebase 添加 iOS 应用:跳过 Firebase SDK 下载按钮
  7. 跳过此 Add initialization code 步骤

    Firebase 添加 iOS 应用:跳过初始化 Code 按钮
  8. 点击 Continue to console 按钮

    Firebase 添加 iOS 应用:前往控制台按钮
  9. 获取您的 iOS Client ID 和 YOUR_DOT_REVERSED_IOS_CLIENT_ID

    1. 前往 Google Cloud Console: console.cloud.google.com

    2. 找到您的项目

      1. 点击项目选择器 Google Cloud Console 项目选择器
      2. 通过 Firebase 项目的准确名称搜索并点击。以我的项目为例,它是 sociallogin-tutorial-app. Firebase 项目选择器项目
    3. 打开搜索栏并打开 credentials

      1. 打开搜索栏 Google Cloud Console 搜索栏
      2. 搜索 credentials 并点击 APIs and Services 其中一个(如截图中的第 2 项) Google Cloud Console 凭据搜索
    4. 点击 iOS client for [YOUR_APP_ID] (auto created by Google Service) 1. 在我的案例中,它是 sociallogin-tutorial-app.

      Google Cloud Console 凭据 iOS Client ID
    5. 复制 Client ID 以及 iOS URL scheme。它们分别是你的 iOSClientId 以及 YOUR_DOT_REVERSED_IOS_CLIENT_ID.

      复制 Google Cloud Console 凭据中的 iOS Client ID
  10. 获取你的 Web Client ID

    1. 返回 Firebase 控制台并前往 Build -> Authentication Firebase Authentication 菜单
    2. 点击 Sign-in method 按钮 Firebase Authentication 登录方法按钮
    3. 点击 Google 提供方 Firebase Authentication 登录方法 Google 提供方
    4. 点击 Web SDK configuration 按钮 Firebase Authentication 登录方式 Web SDK 配置按钮
    5. 复制 Web client ID. 这将是你的 webClientId 在 initialize 插件的 method。 Firebase Authentication 登录方式 Web SDK 配置 Web Client ID
  11. 修改应用的 Info.plist

    1. 打开 Xcode 并找到 Info.plist 文件

      Xcode 项目导航栏中的 Info.plist 文件
    2. 右键点击该文件并以 source code 方式打开

      右键菜单显示 Open As Source Code 选项
    3. 在你的 Plist 文件底部,你会看到一个 </dict> 标签

      Info.plist 文件中的 closing dict 标签
    4. 在 closing 之前插入以下代码片段 </dict> 标签

      Info.plist 中的 URL schemes,code 已插入在 closing dict 标签之前
      <key>CFBundleURLTypes</key>
      <array>
      <dict>
      <key>CFBundleURLSchemes</key>
      <array>
      <string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
      </array>
      </dict>
      </array>
      <key>GIDClientID</key>
      <string>YOUR_IOS_CLIENT_ID.apps.googleusercontent.com</string>
    5. 将 YOUR_DOT_REVERSED_IOS_CLIENT_ID 修改为第 9 步中复制的值(iOS URL scheme)

      Info.plist 中的 URL schemes 已插入实际的反向客户端 ID
  12. 将 YOUR_IOS_CLIENT_ID 更改为第 9 步中复制的 iOS Client ID

  13. 保存文件 Command + S

  14. 修改 AppDelegate.swift

    1. 打开 AppDelegate

      Xcode 项目导航器中的 AppDelegate.swift 文件
    2. 插入 import GoogleSignIn 在文件顶部

      已添加 GoogleSignIn 导入的 AppDelegate.swift
    3. 找到 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) 函数

      AppDelegate 中原始的 application openURL 函数
    4. 按如下方式修改该函数

      func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
      // Called when the app was launched with a url. Feel free to add additional processing here,
      // but if you want the App API to support tracking app url opens, make sure to keep this call
      var handled: Bool
      handled = GIDSignIn.sharedInstance.handle(url)
      if handled {
      return true
      }
      return ApplicationDelegateProxy.shared.application(app, open: url, options: options)
      }
      已添加 GoogleSignIn 处理逻辑的 application openURL 函数
    5. 保存文件 Command + S

  15. 在应用中使用 Google 登录

    至此,您已准备好在应用中使用 Google 登录。 请使用 authUtils.ts 用于 Google 身份验证的示例 App 文件。

首次登录时,系统会自动在 Firebase Auth 中创建用户

如果身份验证卡住或失败:

  • 请检查 idToken audience 是否与您的 Firebase web client ID 一致
  • 检查 Firebase Console 中是否已启用 Google Sign-In
  • 确保 Info.plist 中配置了正确的 URL schemes 和 GIDClientID
  • 请验证 iOSServerClientId 是否与您的 web client ID 一致
  • 请查阅 code 示例应用 供参考

继续配置 iOS 版 Firebase Google Login

标题为“继续配置 iOS 版 Firebase Google Login”

如果你正在使用 iOS 上的 Firebase Google Login 用来规划身份验证和账户流程,并将它与 使用 @capgo/capacitor-social-login 关于使用 @capgo/capacitor-social-login 的原生能力, @capgo/capacitor-social-login 在 @capgo/capacitor-social-login 中查看实现细节, @capgo/capacitor-passkey 有关 @capgo/capacitor-passkey 的实现细节, @capgo/capacitor-native-biometric 以及 @capgo/capacitor-native-biometric 中的实现细节, 双因素认证 有关双重身份验证的实现细节。