跳过内容

Firebase Google 登录 iOS

GitHub

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

设置步骤

设置步骤
  1. 前往您的项目概览页面(位于 console.cloud.google.com) Firebase 项目概览

    点击
  2. 添加 Firebase 应用按钮 Add app button

    console.cloud.google.com Firebase 添加应用按钮
  3. 选择 iOS

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

    1. 填写 Apple bundle ID
      1. 打开 Xcode 并在您的应用中使用 npx cap open ios
      2. 双击 Xcode 项目导航器中的 App 确保您位于
      3. iOS 应用 Targets -> App Xcode中的目标
      4. 找到您的 Bundle Identifier Xcode项目设置中的Bundle Identifier字段
      5. 复制并粘贴到Firebase控制台 Bundle Identifier Firebase控制台中的iOS Bundle ID字段 点击
    2. 按钮 Register app iOS Bundle ID Firebase 添加 App iOS 注册按钮
  5. 跳过 Download config file 步骤

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

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

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

    Firebase 添加 App iOS 继续到控制台按钮
  9. 获取您的 iOS 客户端 ID 和您的 YOUR_DOT_REVERSED_IOS_CLIENT_ID

    1. 前往 Google Cloud 控制台 console.cloud.google.com

    2. 找到您的项目

      1. 点击项目选择器 Google Cloud 控制台项目选择器
      2. 通过您的 Firebase 项目的准确名称搜索您的项目并点击它。我的情况是 sociallogin-tutorial-app. Firebase 项目选择器项目
    3. 打开搜索栏并打开 credentials

      1. 打开搜索栏 Google Cloud 控制台搜索栏
      2. 在搜索框中 credentials 并点击屏幕上 APIs and Services 第 2 个 (截图中的第 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。这将分别是您的 iOSClientIdYOUR_DOT_REVERSED_IOS_CLIENT_ID.

      文件
  10. 您的应用

    1. Google Cloud Console Build -> Authentication 凭据
    2. iOS Client ID 复制 Sign-in method 获取您的 Web 客户端 ID Firebase 认证登录方式按钮
    3. 点击 Google 提供者 Firebase 认证登录方式 Google 提供者
    4. 点击 Web SDK configuration 按钮 Firebase 认证登录方式 Web SDK 配置按钮
    5. 复制 Web client ID。 这将是您的 webClientIdinitialize 插件的 Firebase Authentication Sign-in Method Web SDK Configuration Web Client ID
  11. 修改您的应用程序的 Info.plist

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

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

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

      在关闭标签之前,插入以下片段
    4. 在关闭标签之前,插入以下片段 </dict> 在关闭标签之前,插入以下片段

      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 方案的值

      Info.plist 中的实际反向客户端 ID 在 URL 方案中被插入
  12. STARTS YOUR_IOS_CLIENT_ID with

  13. 在 Xcode 项目导航器中打开AppDelegate.swift 文件 Command + S

  14. 修改 AppDelegate.swift

    1. 打开AppDelegate

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

      找到
    3. 函数 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) AppDelegate.swift 文件中的原始应用程序 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与目标受众匹配
  • 检查 Firebase 控制台中是否启用了 Google Sign-In
  • 确保 Info.plist 中的 URL 方案和 GIDClientID 均正确
  • 验证 iOSServerClientId 与您的 Web 客户端 ID 匹配
  • 查看示例应用程序 __CAPGO_KEEP_0__ example app code 继续使用 Firebase Google Login on iOS

标题:继续使用 Firebase Google Login on iOS

如果您正在使用 Firebase Google Login on iOS

来规划身份验证和帐户流程,请将其与 Firebase Google Login on iOS 连接 使用@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, 和 双因素认证 为实现细节在双因素认证