跳过内容

Firebase Google 登录 on iOS

简介

简介

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

  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. 双击 App 在 Xcode 项目导航器中选择 App
      3. 确保您在 Targets -> App Xcode 中的 Targets 部分,选择 App
      4. 找到您的 Bundle Identifier Bundle Identifier 字段在 Xcode 项目设置中
      5. 复制并粘贴到 Firebase 控制台 Bundle Identifier 复制并粘贴 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 控制台项目选择器
      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) 一个。在我的情况下,它是 sociallogin-tutorial-app.

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

      文件中使用
  10. Google Cloud Console

    1. 获取您的 Web 客户端 ID Build -> Authentication Firebase 认证菜单
    2. 点击 Sign-in method 按钮 Firebase 认证登录方式按钮
    3. 点击 Google 提供者 Firebase 认证登录方式 Google 提供者
    4. 点击 Web SDK configuration 按钮 Firebase Authentication Sign-in Method Web SDK Configuration Button
    5. 复制 Web client ID。 这将是您的 webClientId 在插件的方法中。 initialize Firebase Authentication Sign-in Method Web __CAPGO_KEEP_0__ 配置 Web Client ID Firebase Authentication Sign-in Method Web SDK Configuration Web Client ID
  11. 打开 Xcode 并找到

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

      右键单击此文件并以源代码 __CAPGO_KEEP_0__ 打开
    2. 右键菜单显示 Open As Source code 选项

      Right-click menu showing Open As Source Code option
    3. 标签 Plist 标签 </dict> 在应用程序的 Info.plist 中修改

      关闭 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. 将其更改为步骤 9 中复制的 iOS URL 方案的值 YOUR_DOT_REVERSED_IOS_CLIENT_ID Info.plist 文件中包含实际的反向客户端 ID,位于 URL 方案中

      注意
  12. 更改为 YOUR_IOS_CLIENT_ID 步骤 9 中复制的 iOS 客户端 ID

  13. Command + S

  14. 修改 AppDelegate.swift

    1. 打开 AppDelegate

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

      找到
    3. 函数 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) __CAPGO_KEEP_0__

      原生应用 openURL 函数在 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)
      }
      修改后的应用 openURL 函数,处理了 GoogleSignIn
    5. 保存文件时使用 Command + S

  15. 在应用中使用 Google 登录

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

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

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

  • 确认 idToken 您的 Firebase 网页客户端 ID 与
  • 检查 Firebase 控制台中是否启用了 Google Sign-In
  • 确保 Info.plist 中的 URL 方案和 GIDClientID 均正确
  • 确认 iOSServerClientId 与您的网页客户端 ID 匹配
  • 查看 示例应用程序 code 以获取参考