跳过内容

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 确保您位于
      3. 确保您位于 Targets -> App Xcode中的目标
      4. 找到您的 Bundle Identifier Xcode项目设置中的Bundle Identifier字段
      5. 复制并粘贴到Firebase控制台 Bundle Identifier Firebase控制台中的iOS Bundle ID添加应用 点击
    2. 按钮 Register app targetLanguage 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 项 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.

      获取您的 web 客户端 ID
  10. 返回到 Firebase 控制台并转到

    1. Firebase 身份验证菜单 Build -> Authentication 点击
    2. 按钮 Sign-in method Google Cloud Console Credentials iOS Client ID 复制 Firebase 认证登录方式按钮
    3. 点击 Google 提供者 Firebase 认证登录方式 Google 提供者
    4. 点击 Web SDK configuration 按钮 Firebase 认证登录方式 Web SDK 配置按钮
    5. 复制 Web client ID。 这将是您的 webClientIdinitialize 插件的登录方式 Firebase 认证登录方式 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> 在关闭标签之前,插入以下片段

      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. 确保这个值 YOUR_IOS_CLIENT_ID

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

  14. 修改 AppDelegate.swift

    1. 打开AppDelegate

      在 Xcode 项目导航器中打开AppDelegate.swift 文件
    2. 插入 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 匹配
  • 查看 示例应用程序 code 以参考

继续从 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,并且 双因素认证 为实现细节在双因素认证