跳到内容

iOS 上的 Google 登录

GitHub

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

在 iOS 上使用 Google 登录

使用 Google 登录在 iOS 上

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

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

    1. 在搜索栏中点击

      Google 控制台搜索栏
    2. 在搜索栏中输入 credentials 并点击 APIs and Services 第 2 项(截图中的第 2 项)

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

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

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

      应用类型选择菜单中 iOS 选项高亮
    6. 找到 bundle ID

      1. 打开 Xcode

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

        App 目标
      3. 确保您在 Xcode 中的 Targets -> App

        Targets 部分中 App 选中
      4. 找到你的 Bundle Identifier

        在 Xcode 项目设置中找到 Bundle Identifier 字段
      5. 返回 Google Console,粘贴你的 Bundle IdentifierBundle ID

        Google Console iOS 客户端创建表单中的 Bundle ID 字段
    7. 如果你已经发布了你的应用到 App Store,_optionally_ 添加你的 App Store IDTeam ID 到客户端 ID 中,如果你已经发布了你的应用到 App Store

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

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

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

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

      客户端ID详细信息显示客户端ID和反向客户端ID以复制
  2. 修改应用的 Info.plist

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

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

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

      在关闭标签之前,插入以下片段
    4. 在 Info.plist 文件的底部,您将看到一个 </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>
    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 文件顶部

      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

  4. Setup Google login in your JavaScript/TypeScript code

    1. 导入 SocialLogin 复制到剪贴板 Capacitor

      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
      }
      })
      })

      复制到剪贴板

      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. Implement the login function. Create a button and run the following code on click

      实现登录功能。创建一个按钮并在点击时运行以下

      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 登录插件与 @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 登录继续

如果您正在使用

Google 登录 Google 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 中的实现细节, 双因素认证 在双因素认证中的实现细节.