Google 登录在 iOS 上
复制一个包含安装步骤和完整Markdown指南的设置提示。
简介
标题为“简介”在本指南中,您将学习如何设置 Google 登录和 Capgo 社交登录 iOS。 我假设您已经阅读了 通用设置指南.
在 iOS 中使用 Google 登录
标题为“在 iOS 中使用 Google 登录”在本部分中,您将学习如何在 iOS 中设置 Google 登录。
-
在 Google 控制台中创建 iOS 客户端 ID
-
点击搜索栏
-
在搜索框中
credentials并点击截图中的第二个选项APIs and Services截图中第二个选项(第 2 项)
-
点击 Google 控制台中的
create credentials
-
选择
OAuth client ID
-
选择
Application type到iOS
-
找到 bundle ID
-
打开 Xcode
-
双击 Xcode 项目导航器中的
App
-
在 Xcode 中选择 App 的
Targets -> App
-
在 Xcode 项目设置中的
Bundle Identifier
-
到
Bundle Identifier可选地,添加您的Bundle ID
-
-
__CAPGO_KEEP_0__
App Store ID__CAPGO_KEEP_0__Team ID如果您已经将应用程序发布到 App Store,则需要将此客户端 ID 输入到客户端 ID 中 -
填写所有详细信息后,点击
create
-
点击
OK
-
打开新创建的 iOS 客户端
-
复制以下数据
-
-
修改您的应用程序的Info.plist
-
打开Xcode并找到
Info.plist文件
-
右键单击此文件并以源代码code打开
-
您的应用程序的底部
Plist您将看到一个文件</dict>标签
-
在关闭标签之前,插入以下片段
</dict>Info.plist 文件,包含 URL 方案 __CAPGO_KEEP_0__ 在关闭字典标签之前
<key>CFBundleURLTypes</key><array><dict><key>CFBundleURLSchemes</key><array><string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string></array></dict></array> -
Info.plist 文件,包含实际反转的客户端 ID 在 URL 方案中
YOUR_DOT_REVERSED_IOS_CLIENT_ID注意
-
保存文件
Command + S
-
-
修改
AppDelegate.swift-
在 Xcode 项目导航器中打开 AppDelegate 文件
-
在 AppDelegate.swift 文件中添加 GoogleSignIn 导入
import GoogleSignIn找到
-
__CAPGO_KEEP_0__
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:])__CAPGO_KEEP_0__
-
修改函数使其类似于此
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 中设置 Google 登录(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}})})__CAPGO_KEEP_0__
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/privacy-screen当同时使用这两个插件时,Google 登录 webview 将被隐私屏幕中断。
解决方法: 调用 await PrivacyScreen.disable(); __CAPGO_KEEP_0__:
import { PrivacyScreen } from '@capacitor/privacy-screen';import { SocialLogin } from '@capgo/capacitor-social-login';
await PrivacyScreen.disable();await SocialLogin.login({ provider: 'google', options: {}});