跳转到正文

iOS 上的 Firebase Google Login

GitHub

本指南将帮助你在 iOS 上集成 Google 登录与 Firebase 身份验证。假设你已完成 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 目标
      3. 确保您位于 Targets -> App Xcode 中的 Targets 部分,且已选中 App
      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 客户端 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) 一个。在我的情况下,它是 sociallogin-tutorial-app.

      Google Cloud Console 凭据 iOS 客户端 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 插件方法。 Firebase Authentication 登录方式 Web SDK 配置 Web Client ID
  11. 修改应用的 Info.plist

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

      Xcode 项目导航器中的 Info.plist 文件
    2. 右键单击此文件,选择“以源码打开 code”

      显示“以源码打开 Code”选项的右键菜单
    3. 在您的 Plist 文件底部,您会看到 </dict> 标签

      Info.plist 文件中的闭合字典标签
    4. 在闭合 </dict> 标签

      在闭合字典标签前插入 URL schemes code 的 Info.plist
      <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 import 的 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 进行身份验证。

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

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

  • 请检查 idToken audience 与你的 Firebase Web 客户端 ID 匹配
  • 检查 Firebase Console 中是否已启用 Google Sign-In
  • 确保 Info.plist 包含正确的 URL schemes 和 GIDClientID
  • 验证 iOSServerClientId 与您的 Web 客户端 ID 匹配
  • 请查阅 示例应用 code 仅供参考

如果你正在使用 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 中的实现细节, 双因素认证 有关双重身份验证的实现细节。