Google iOS登录
复制一个包含安装步骤和此插件的完整Markdown指南的设置提示
介绍
标题为“介绍”在本指南中,您将学习如何在 iOS 设备上设置 Google 登录和 Capgo 社交登录。 我假设您已经阅读了.
使用 Google 登录在 iOS 设备上
标题为“使用 Google 登录在 iOS 设备上”在本部分中,您将学习如何在iOS中设置Google登录。
-
在Google控制台中创建一个iOS客户端ID
-
点击搜索栏
-
搜索
credentials并点击APIs and Services第一个(截图中的第2个)
-
点击
create credentials
-
选择
OAuth client ID
-
选择
Application type到iOS
-
找到 Bundle ID
-
打开 Xcode
-
双击 Xcode 项目导航器中的
App
-
找到 Xcode 项目设置中的
Targets -> App
-
context
Bundle Identifier
-
context
Bundle Identifier进入Bundle ID
-
-
或者
App Store ID或者Team ID进入客户端 ID -
填写所有详细信息后,点击
create
-
点击
OK
-
打开刚创建的 iOS 客户端
-
复制以下数据
-
-
打开 Xcode 并找到
-
文件
Info.plist客户端 ID
-
右键单击此文件并以源代码形式打开code
-
您的
Plist文件底部,您将看到一个</dict>标签
-
在关闭的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
-
-
Setup Google login in your JavaScript/TypeScript code
-
导入
SocialLogin并且Capacitorimport { SocialLogin } from '@capgo/capacitor-social-login';import { Capacitor } from '@capacitor/core'; -
调用初始化方法(仅需调用一次)
基本设置(在线模式 - 大多数应用程序推荐):
// onMounted is Vue specificonMounted(() => {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 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登录流程正常工作
-
已知问题
已知问题隐私屏幕插件不兼容性
Google登录插件与隐私屏幕插件不兼容 @capacitor/隐私屏幕使用两者同时时,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: {}});继续从Google Login on iOS
如果您正在使用Google Login on iOS 来规划认证和账户流程, 与@__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-social-login进行连接 @capgo/capacitor-social-login 为使用 @capgo/capacitor-social-login 的原生能力 @capgo/capacitor-社交登录 为 @capgo/capacitor-social-login 的实现细节 @capgo/capacitor-passkey 为 @capgo/capacitor-passkey 的实现细节 为 @capgo/capacitor-native-biometric 的实现细节 对于在 @capgo/capacitor-native-biometric 中的实现细节 为两因素认证的实现细节 编辑页面