跳转到内容

iOS 上的 Firebase Google 登录

本指南将帮助你在 iOS 上集成带有 Firebase Authentication 的 Google 登录。我假设你已经完成了通用 Firebase Google 设置

  1. console.cloud.google.com 转到项目概览

    Firebase Project Overview
  2. 点击 Add app 按钮

    Firebase Add App Button Firebase Add App Button
  3. 选择 iOS

    Firebase Add App iOS Button
  4. 填写表单的第一部分

    1. 填写 Apple bundle ID
      1. 使用 npx cap open ios 在 Xcode 中打开你的应用
      2. 双击 App App target in Xcode project navigator
      3. 确保你在 Targets -> AppTargets section in Xcode with App selected
      4. 找到你的 Bundle Identifier Bundle Identifier field in Xcode project settings
      5. 复制 Bundle Identifier 并在 Firebase 控制台中粘贴 Firebase Add App iOS Bundle ID Field
    2. 点击 Register app 按钮 Firebase Add App iOS Register Button
  5. 跳过 Download config file 步骤

    Firebase Add App iOS Skip Download Button
  6. 跳过 Add firebase SDK 步骤

    Firebase Add App iOS Skip Download Firebase SDK Button
  7. 跳过 Add initialization code 步骤

    Firebase Add App iOS Skip Add Initialization Code Button
  8. 点击 Continue to console 按钮

    Firebase Add App iOS Continue to Console Button
  9. 获取你的 iOS 客户端 ID 和 YOUR_DOT_REVERSED_IOS_CLIENT_ID

    1. console.cloud.google.com 转到 Google Cloud Console

    2. 找到你的项目

      1. 点击项目选择器 Google Cloud Console Project Selector
      2. 按 Firebase 项目的确切名称搜索你的项目并点击它。在我的情况下,它是 sociallogin-tutorial-appFirebase Project Selector Project
    3. 打开搜索栏并打开 credentials

      1. 打开搜索栏 Google Cloud Console Search Bar
      2. 搜索 credentials 并点击 APIs and Services 那个(截图上的第 2 个) Google Cloud Console Credentials Search
    4. 点击 iOS client for [YOUR_APP_ID] (auto created by Google Service) 那个。在我的情况下,它是 sociallogin-tutorial-app

      Google Cloud Console Credentials iOS Client ID
    5. 复制 Client IDiOS URL scheme。这将分别是你的 iOSClientIdYOUR_DOT_REVERSED_IOS_CLIENT_ID

      Google Cloud Console Credentials iOS Client ID Copy
  10. 获取你的 Web 客户端 ID

    1. 返回 Firebase 控制台并转到 Build -> Authentication Firebase Authentication Menu
    2. 点击 Sign-in method 按钮 Firebase Authentication Sign-in Method Button
    3. 点击 Google 提供商 Firebase Authentication Sign-in Method Google Provider
    4. 点击 Web SDK configuration 按钮 Firebase Authentication Sign-in Method Web SDK Configuration Button
    5. 复制 Web client ID。这将是插件的 initialize 方法中的 webClientIdFirebase Authentication Sign-in Method Web SDK Configuration Web Client ID
  11. 修改应用的 Info.plist

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

      Info.plist file in Xcode project navigator
    2. 右键单击此文件并将其作为源代码打开

      Right-click menu showing Open As Source Code option
    3. Plist 文件的底部,你将看到一个 </dict> 标签

      Closing dict tag in Info.plist file
    4. 在关闭的 </dict> 标签之前插入以下片段

      Info.plist with URL schemes code inserted before closing dict tag
      <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 with actual reversed client ID inserted in URL schemes
  12. YOUR_IOS_CLIENT_ID 更改为你在步骤 9 中复制的 iOS 客户端 ID

  13. 使用 Command + S 保存文件

  14. 修改 AppDelegate.swift

    1. 打开 AppDelegate

      AppDelegate.swift file in Xcode project navigator
    2. 在文件顶部插入 import GoogleSignIn

      AppDelegate.swift with GoogleSignIn import added
    3. 找到 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) 函数

      Original application openURL function in AppDelegate
    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)
      }
      Modified application openURL function with GoogleSignIn handling
    5. 使用 Command + S 保存文件

  15. 在应用中使用 Google 登录

    在此步骤,你已准备好在应用中使用 Google 登录。 请使用示例应用的 authUtils.ts 文件通过 Google 进行身份验证。

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

如果身份验证挂起或失败:

  • 验证 idToken audience 是否与你的 Firebase Web 客户端 ID 匹配
  • 检查 Firebase Console 中是否启用了 Google 登录
  • 确保 Info.plist 具有正确的 URL schemes 和 GIDClientID
  • 验证 iOSServerClientId 是否与你的 Web 客户端 ID 匹配
  • 查看示例应用代码以供参考