跳过内容

Firebase Google 登录 iOS

GitHub

本指南将帮助您在 iOS 上将 Google Sign-In 与 Firebase Authentication 集成。假设您已经完成了 通用 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. 双击 Xcode 项目导航器中的 App 目标 App 确保您在 Xcode 的 Targets 部分中选中了 App
      3. 在 Xcode Targets 部分中选中 App Targets -> App 在 Xcode Targets 部分中选中 App
      4. 找到你的 Bundle Identifier Bundle Identifier字段在Xcode项目设置中
      5. 复制 Bundle Identifier 并粘贴到Firebase控制台 Firebase添加AppiOS Bundle ID字段
    2. 点击 Register app 按钮 Firebase添加AppiOS注册按钮
  5. 跳过 Download config file 步骤

    Firebase Add App iOS 跳过下载按钮
  6. 跳过 Add firebase SDK 步骤

    Firebase Add App iOS 跳过下载 Firebase SDK 按钮
  7. 跳过 Add initialization code 步骤

    Firebase Add App iOS 跳过添加初始化 Code 按钮
  8. 点击 Continue to console 按钮

    Firebase Add App 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) 截图中第 1 项。在我的情况下是 sociallogin-tutorial-app.

      Google Cloud Console 凭证 iOS 客户端 ID
    5. 复制 Client ID 以及 iOS URL scheme。这将分别是你的 iOSClientIdYOUR_DOT_REVERSED_IOS_CLIENT_ID.

      Google Cloud Console Credentials iOS Client ID 复制
  10. 获取您的 Web 客户端 ID

    1. 返回到 Firebase 控制台并转到 Build -> Authentication Firebase 身份验证菜单
    2. 点击 Sign-in method 按钮 Firebase 身份验证登录方式按钮
    3. 点击 Google 服务提供商 Firebase Authentication Sign-in Method Google Provider
    4. 点击 Web SDK configuration 按钮 Firebase Authentication Sign-in Method Web SDK 配置按钮
    5. 复制 Web client ID. 这将是你的 webClientIdinitialize 插件的方法中. Firebase Authentication Sign-in Method Web SDK 配置 Web Client ID
  11. 修改你的 app 的 Info.plist

    1. 打开 Xcode 并在 Xcode 项目导航器中找到文件 Info.plist __CAPGO_KEEP_0__

      Xcode 项目导航器中找到 Info.plist 文件
    2. 右键点击此文件并以源代码形式打开 code

      右键菜单显示以源代码形式打开 Code 选项
    3. 您的文件底部会看到一个 Plist Info.plist 文件中的 </dict> Info.plist 文件中的

      在关闭
    4. Info.plist 文件中插入以下片段,紧接着关闭 </dict> Info.plist 文件中插入 URL 方案 __CAPGO_KEEP_0__,紧接着关闭

      Info.plist 文件中插入 URL 方案 code
      <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 文件中,实际的反转客户端 ID 插入到 URL_scheme 中
  12. 修改为步骤 9 中复制的 iOS Client ID YOUR_IOS_CLIENT_ID 保存文件

  13. 确保这个值以 Command + S

  14. 修改 AppDelegate.swift

    1. 打开AppDelegate

      在Xcode项目导航器中打开AppDelegate.swift文件
    2. import GoogleSignIn AppDelegate.swift文件的顶部添加

      AppDelegate.swift文件中添加了GoogleSignIn import
    3. 找到 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) 函数

      AppDelegate中的原始应用程序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)
      }
      修改后的应用程序openURL函数,处理GoogleSignIn
    5. 保存文件为 Command + S

  15. 在您的应用程序中使用Google登录

    您已经准备好在应用程序中使用Google登录。 请使用示例应用程序的 authUtils.ts 文件与Google进行身份验证。

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

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

  • 验证 idToken 确保
  • 与Firebase Web客户端ID匹配
  • 确保 Info.plist 中的 URL 方案和 GIDClientID 正确
  • 验证 iOSServerClientId 与您的 Web 客户端 ID 匹配
  • 查看 示例应用程序 code 以参考

继续 Firebase Google 登录

标题:继续 Firebase Google 登录

如果您正在使用 Firebase Google 登录 来规划身份验证和帐户流程,连接它 使用 @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的实现细节, 双因素认证 双因素认证的实现细节