Firebase Google 登录 on iOS
复制一个安装步骤和完整的Markdown指南的设置提示。
简介
简介本指南将帮助您在 iOS 上将 Google Sign-In 与 Firebase Authentication 集成。假设您已经完成了 __CAPGO_KEEP_0__.
-
前往您的项目概览 console.cloud.google.com
-
点击
Add app按钮
-
选择
iOS
-
填写表单的第一部分
- 填写
Apple bundle ID- 打开 Xcode 并在您的应用中
npx cap open ios - 双击
App
- 确保您在
Targets -> App
- 找到您的
Bundle Identifier
- 复制并粘贴到 Firebase 控制台
Bundle Identifier复制并粘贴
- 打开 Xcode 并在您的应用中
- 点击
Register app按钮
- 填写
-
跳过
Download config file步骤
-
跳过
Add firebase SDK步骤
-
跳过
Add initialization code步骤
-
点击
Continue to console按钮
-
获取您的 iOS 客户端 ID 和您的
YOUR_DOT_REVERSED_IOS_CLIENT_ID-
前往 Google Cloud 控制台 console.cloud.google.com
-
找到您的项目
- 点击项目选择器
- 通过您的 Firebase 项目的准确名称搜索您的项目,然后点击它。在我的情况下,它是
sociallogin-tutorial-app.
- 点击项目选择器
-
打开搜索栏并打开
credentials- 打开搜索栏
- 搜索
credentials并点击APIs and Services截图中的第二个(第 2 个)
- 打开搜索栏
-
点击
iOS client for [YOUR_APP_ID] (auto created by Google Service)一个。在我的情况下,它是sociallogin-tutorial-app.
-
复制
Client ID以及iOS URL scheme将分别是您的iOSClientId和YOUR_DOT_REVERSED_IOS_CLIENT_ID.
-
-
Google Cloud Console
- 获取您的 Web 客户端 ID
Build->Authentication
- 点击
Sign-in method按钮
- 点击
Google提供者
- 点击
Web SDK configuration按钮
- 复制
Web client ID。 这将是您的webClientId在插件的方法中。initializeFirebase Authentication Sign-in Method Web __CAPGO_KEEP_0__ 配置 Web Client ID
- 获取您的 Web 客户端 ID
-
打开 Xcode 并找到
-
文件
Info.plist在 Xcode 项目导航器中找到 Info.plist 文件
-
右键菜单显示 Open As Source code 选项
-
标签
Plist标签</dict>在应用程序的 Info.plist 中修改
-
在关闭标签之前插入以下片段
</dict>标签
<key>CFBundleURLTypes</key><array><dict><key>CFBundleURLSchemes</key><array><string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string></array></dict></array><key>GIDClientID</key><string>YOUR_IOS_CLIENT_ID.apps.googleusercontent.com</string> -
将其更改为步骤 9 中复制的 iOS URL 方案的值
YOUR_DOT_REVERSED_IOS_CLIENT_IDInfo.plist 文件中包含实际的反向客户端 ID,位于 URL 方案中
-
-
更改为
YOUR_IOS_CLIENT_ID步骤 9 中复制的 iOS 客户端 ID -
以
Command + S -
修改
AppDelegate.swift-
打开 AppDelegate
-
在文件顶部
import GoogleSignInAppDelegate.swift 文件中添加了 GoogleSignIn 导入
-
函数
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
-
-
在应用中使用 Google 登录
在此步骤,您已经准备好在应用中使用 Google 登录。 请使用 authUtils.ts 示例应用中的文件以使用 Google 登录进行身份验证。
在 Firebase Auth 中,用户将在首次登录时自动创建。
故障排除
标题为“故障排除”如果身份验证卡住或失败:
- 确认
idToken您的 Firebase 网页客户端 ID 与 - 检查 Firebase 控制台中是否启用了 Google Sign-In
- 确保 Info.plist 中的 URL 方案和 GIDClientID 均正确
- 确认
iOSServerClientId与您的网页客户端 ID 匹配 - 查看 示例应用程序 code 以获取参考