跳转到正文

iOS 端 Google Login

GitHub

本指南将介绍如何在 iOS 上为 Capgo Social Login 配置 Google 登录。 假设你已经阅读过 通用设置指南.

在本部分中,您将学习如何在 iOS 中设置 Google 登录。

  1. 在 Google 控制台中创建 iOS 客户端 ID

    1. 点击搜索栏

      Google Console 搜索栏
    2. 搜索 credentials 并点击 APIs and Services 一个(截图中的编号 2)

      搜索结果中显示凭据选项,其中 APIs and Services 已高亮
    3. 点击 create credentials

      Google Console 中的 Create credentials 按钮
    4. 选择 OAuth client ID

      凭据创建菜单中的 OAuth client ID 选项
    5. 选择 Application type 改为 iOS

      应用类型选择,iOS 选项高亮显示
    6. 查找 Bundle ID

      1. 打开 Xcode

      2. 双击 App

        Xcode 项目导航器中的 App 目标
      3. 确保你位于 Targets -> App

        Xcode 的 Targets 部分,且已选中 App
      4. 找到你的 Bundle Identifier

        Xcode 项目设置中的 Bundle Identifier 字段
      5. 返回 Google Console,粘贴您的 Bundle Identifier 为 Bundle ID

        Google Console iOS 客户端创建表单中的 Bundle ID 字段
    7. 可选:将您的 App Store ID 或 Team ID 添加到 Client ID 中(如果您已将应用发布至 App Store)

    8. 填写完所有详情后,点击 create

      iOS 客户端创建表单底部的 Create 按钮
    9. 点击 OK

      Client ID 创建成功确认对话框上的 OK 按钮
    10. 打开新创建的 iOS 客户端

      凭据列表中新创建的 iOS 客户端
    11. 复制以下数据

      Client ID 详情,显示 Client ID 和反向 Client ID,可复制
  2. 修改应用的 Info.plist

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

      Xcode 项目导航器中的 Info.plist 文件
    2. 右键单击此文件,选择“以源码打开 code”

      显示“以源码打开 Code”选项的右键菜单
    3. 在您的 Plist 文件底部,您会看到一个 </dict> 标签

      Info.plist 文件中的闭合字典标签
    4. 在闭合 </dict> 标签

      在闭合字典标签前插入了 URL schemes code 的 Info.plist
      <key>CFBundleURLTypes</key>
      <array>
      <dict>
      <key>CFBundleURLSchemes</key>
      <array>
      <string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
      </array>
      </dict>
      </array>
    5. 更改 YOUR_DOT_REVERSED_IOS_CLIENT_ID 为上一步复制的值

      在 Info.plist 的 URL schemes 中填入实际的反转客户端 ID
    6. 保存文件,内容如下 Command + S

  3. 修改 AppDelegate.swift

    1. 打开 AppDelegate

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

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

      AppDelegate 中原始的 application 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)
      }
      已添加 GoogleSignIn 处理逻辑的 application openURL 函数
    5. 保存文件,内容如下 Command + S

  4. 在你的 JavaScript/TypeScript code 中配置 Google 登录

    1. 导入 SocialLogin 以及 Capacitor

      import { SocialLogin } from '@capgo/capacitor-social-login';
      import { Capacitor } from '@capacitor/core';
    2. 调用 initialize 方法(此方法应仅调用一次)

      基础配置(在线模式 - 推荐大多数应用使用):

      // onMounted is Vue specific
      onMounted(() => {
      SocialLogin.initialize({
      google: {
      iOSClientId: '673324426943-redacted.apps.googleusercontent.com',
      mode: 'online' // Default mode
      }
      })
      })

      使用额外 Client ID 的高级配置:

      onMounted(() => {
      SocialLogin.initialize({
      google: {
      webClientId: 'YOUR_WEB_CLIENT_ID', // Optional: for web platform support
      iOSClientId: 'YOUR_IOS_CLIENT_ID', // Required: from step 1
      iOSServerClientId: 'YOUR_WEB_CLIENT_ID', // Optional: same as webClientId, needed for some advanced features
      mode: 'online' // 'online' or 'offline'
      }
      })
      })
    3. 实现登录功能。创建一个按钮,并在点击时运行以下 code

      对于在线模式:

      const res = await SocialLogin.login({
      provider: 'google',
      options: {}
      })
      // handle the response - contains user data
      console.log(JSON.stringify(res))

      离线模式:

      const res = await SocialLogin.login({
      provider: 'google',
      options: {
      forceRefreshToken: true // Recommended for offline mode
      }
      })
      // res contains serverAuthCode, not user data
      // Send serverAuthCode to your backend to get user information
      // Do not call SocialLogin.refresh() in offline mode
      console.log('Server auth code:', res.result.serverAuthCode)
  5. 测试你的应用

    1. 构建应用并运行 cap sync

    2. 如果一切配置正确,你应该能看到 Google 登录流程正常运行

      iOS 上 Google 登录流程演示,展示登录过程及成功认证

Google Login 插件与以下插件不兼容: @capacitor/privacy-screen. 同时使用这两个插件时,Google 登录的 webview 会被 Privacy Screen 中断。

变通方案: 调用 await PrivacyScreen.disable(); 调用登录函数前:

import { PrivacyScreen } from '@capacitor/privacy-screen';
import { SocialLogin } from '@capgo/capacitor-social-login';
await PrivacyScreen.disable();
await SocialLogin.login({
provider: 'google',
options: {}
});

在 iOS 上继续 Google Login 流程

标题为“在 iOS 上继续 Google Login 流程”

如果你正在使用 iOS 端 Google Login 用来规划身份验证和账户流程,并将它与 使用 @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 中的实现细节, 双因素认证 有关双重身份验证的实现细节。