iOS 端 Google Login
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
本指南将介绍如何在 iOS 上为 Capgo Social Login 配置 Google 登录。 假设你已经阅读过 通用设置指南.
在 iOS 上使用 Google 登录
标题为“在 iOS 上使用 Google 登录”的章节在本部分中,您将学习如何在 iOS 中设置 Google 登录。
-
在 Google 控制台中创建 iOS 客户端 ID
-
点击搜索栏
-
搜索
credentials并点击APIs and Services一个(截图中的编号 2)
-
点击
create credentials
-
选择
OAuth client ID
-
选择
Application type改为iOS
-
查找 Bundle ID
-
打开 Xcode
-
双击
App
-
确保你位于
Targets -> App
-
找到你的
Bundle Identifier
-
返回 Google Console,粘贴您的
Bundle Identifier为Bundle ID
-
-
可选:将您的
App Store ID或Team ID添加到 Client ID 中(如果您已将应用发布至 App Store) -
填写完所有详情后,点击
create
-
点击
OK
-
打开新创建的 iOS 客户端
-
复制以下数据
-
-
修改应用的 Info.plist
-
打开 Xcode 并找到
Info.plist文件
-
右键单击此文件,选择“以源码打开 code”
-
在您的
Plist文件底部,您会看到一个</dict>标签
-
在闭合
</dict>标签
<key>CFBundleURLTypes</key><array><dict><key>CFBundleURLSchemes</key><array><string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string></array></dict></array> -
更改
YOUR_DOT_REVERSED_IOS_CLIENT_ID为上一步复制的值
-
保存文件,内容如下
Command + S
-
-
修改
AppDelegate.swift-
打开 AppDelegate
-
插入
import GoogleSignIn在文件顶部
-
找到
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:])函数
-
将函数改成这样
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 callvar handled: Boolhandled = GIDSignIn.sharedInstance.handle(url)if handled {return true}return ApplicationDelegateProxy.shared.application(app, open: url, options: options)}
-
保存文件,内容如下
Command + S
-
-
在你的 JavaScript/TypeScript code 中配置 Google 登录
-
导入
SocialLogin以及Capacitorimport { SocialLogin } from '@capgo/capacitor-social-login';import { Capacitor } from '@capacitor/core'; -
调用 initialize 方法(此方法应仅调用一次)
基础配置(在线模式 - 推荐大多数应用使用):
// onMounted is Vue specificonMounted(() => {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 supportiOSClientId: 'YOUR_IOS_CLIENT_ID', // Required: from step 1iOSServerClientId: 'YOUR_WEB_CLIENT_ID', // Optional: same as webClientId, needed for some advanced featuresmode: 'online' // 'online' or 'offline'}})}) -
实现登录功能。创建一个按钮,并在点击时运行以下 code
对于在线模式:
const res = await SocialLogin.login({provider: 'google',options: {}})// handle the response - contains user dataconsole.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 modeconsole.log('Server auth code:', res.result.serverAuthCode)
-
-
测试你的应用
-
构建应用并运行
cap sync -
如果一切配置正确,你应该能看到 Google 登录流程正常运行
-
已知问题
标题为“已知问题”的章节Privacy Screen 插件不兼容
标题为“Privacy Screen 插件不兼容”的章节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 中的实现细节, 双因素认证 有关双重身份验证的实现细节。