跳至内容

iOS 上的 Google 登录

在本指南中,您将学习如何使用Capgo Social Login for iOS设置Google登录。 我假设您已经阅读了 一般设置指南.

在 iOS 中使用 Google 登录

在 iOS 中使用 Google 登录

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

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

    1. 点击搜索栏

      Google 控制台搜索栏
    2. 在 Google 控制台中搜索 credentials 并点击 APIs and Services 截图中第 2 个选项(数字 2)

      显示凭据选项的搜索结果,APIs 和 Services 高亮显示
    3. 点击 create credentials

      在 Google 控制台中创建凭据按钮
    4. 选择 OAuth client ID

      在凭据创建菜单中选择 OAuth 客户端 ID
    5. 选择 Application typeiOS

      应用类型选择菜单中 iOS 选项突出显示
    6. 找到 bundle ID

      1. 打开 Xcode

      2. 双击 Xcode 项目导航器中的 App 目标 App

        确保您在 Xcode 中
      3. 目标部分中 App 选中 Targets -> App

        找到您的
      4. 找到您的 Bundle Identifier

        Xcode项目设置中的Bundle Identifier字段
      5. 返回Google控制台,粘贴你的 Bundle IdentifierBundle ID

        在Google控制台iOS客户端创建表单中的Bundle ID字段中粘贴你的
    7. 如果你已经将应用程序发布到App Store,则可选地添加你的 App Store IDTeam ID 到客户端ID中

    8. 填写所有详细信息后,点击 create

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

      客户端ID创建确认对话框中的确定按钮
    10. 打开新创建的iOS客户端

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

      客户端ID详细信息显示客户端ID和反转客户端ID以复制
  2. 在这个

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

      Xcode 项目导航器中找到 Info.plist 文件
    2. 右键单击此文件并以源代码打开 code

      右键菜单显示以源代码打开 Code 选项
    3. 您的文件底部会看到一个 Plist Info.plist 文件中的 </dict> 关闭字典标签

      在关闭字典标签之前,插入以下片段
    4. Info.plist 文件中插入 URL 方案 __CAPGO_KEEP_0__ </dict> Info.plist 文件中插入 URL 方案 __CAPGO_KEEP_0__

      Info.plist 文件中插入 URL 方案 code
      <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中实际的反转客户端ID插入URL方案
    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中的原始应用程序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

  4. Setup JavaScript/TypeScript中的Google登录code

    1. 导入 SocialLoginCapacitor

      import { SocialLogin } from '@capgo/capacitor-social-login';
      import { Capacitor } from '@capacitor/core';
    2. 调用初始化方法 (仅需调用一次)

      基本设置 (在线模式 - 大多数应用程序的推荐设置):

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

      高级设置 (附加客户端 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))

      For offline mode:

      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 登录插件与 @capacitor/privacy-screen当同时使用这两个插件时,Google 登录 webview 将被隐私屏幕中断。

解决方法: 在调用登录函数之前调用 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 登录

继续使用 iOS 上的 Google 登录 如果您正在使用 规划身份验证和帐户流程,连接它 使用@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的实现细节 两因素身份验证 两因素身份验证的实现细节