跳过内容

Firebase Google 登录 on iOS

GitHub

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

  1. 前往您的项目概览 console.cloud.google.com

    Firebase 项目概览
  2. 点击 Add app button

    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 添加 App iOS Bundle ID 字段
    2. 点击 Register app 按钮 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 Console 项目选择器
      2. 在 Google Cloud Console 中,搜索 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 Credentials iOS Client 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 身份验证登录方式 Google 提供者
    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. 修改你的应用程序的 Info.plist

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

      Info.plist 文件在 Xcode 项目导航器
    2. Right click this file and open it as source code

      右键菜单显示 Open As Source Code 选项
    3. 在文件底部,您会看到一个 Plist 标签 </dict> Info.plist 文件中的关闭字典标签

      在 Info.plist 文件中的关闭字典标签之前,插入以下片段
    4. Info.plist 文件,包含 URL 方案 __CAPGO_KEEP_0__ </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. Info.plist 文件,包含实际的反向客户端 ID YOUR_DOT_REVERSED_IOS_CLIENT_ID 注意事项

      在文件底部,您会看到一个
  12. 更改 YOUR_IOS_CLIENT_ID 更改为您在第 9 步中复制的 iOS Client ID

  13. Command + S

  14. 修改 AppDelegate.swift

    1. 打开

      AppDelegate.swift 文件在 Xcode 项目导航器中
    2. 在文件顶部 import GoogleSignIn 插入

      AppDelegate.swift 中添加了 GoogleSignIn 导入
    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 与
  • 检查 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的实现细节 双因素认证 查看双因素认证的实现细节