跳转至正文

iOS 端 Google Login

GitHub

在本指南中,你将学习如何通过 Capgo Social Login 在 iOS 上配置 Google Login。 我假设你已经阅读了 通用设置指南.

在 iOS 上使用 Google Login

“Using Google login on iOS” 章节

在本节中,你将学习如何在 iOS 上配置 Google 登录。

  1. 在 Google Console 中创建 iOS Client ID

    1. 点击搜索栏

      Google Console 搜索栏
    2. 搜索 credentials 然后点击 APIs and Services 这一项(截图中编号 2)

      搜索结果显示已高亮“APIs and Services”下的“Credentials”选项
    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 target
      3. 确保你处于 Targets -> App

        Xcode 中已选中 App 的 Targets 部分
      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 的详情,方便复制
  2. 修改应用的 Info.plist

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

      Xcode 项目导航器中的 Info.plist 文件
    2. 右键点击该文件,并选择以源码方式打开 code

      右键菜单中显示 Open As Source Code 选项
    3. 在文件的底部 Plist 你会看到一个 </dict> 标签

      Info.plist 文件中的 closing dict 标签
    4. 请在 closing 标签之前插入以下代码片段 </dict> 标签

      在 closing dict 标签前插入带有 code URL schemes 的 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 粘贴至上一步复制的值

      在 URL schemes 中插入实际的反向客户端 ID 的 Info.plist
    6. 保存文件,包含 Command + S

  3. 修改 AppDelegate.swift

    1. 打开 AppDelegate

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

      已添加 GoogleSignIn 导入的 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 方法(仅需调用一次)

      基础配置(在线模式 - 大多数 App 的推荐选择):

      // 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 规划身份验证和账户流程,并将它与 使用 @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 中的实现细节,以及 双重身份验证 了解双因素认证中的实现细节。