🔐
SociallySwiftUI 的社交登录集成
功能
- Google 登录集成
- Facebook 登录集成
示例
要运行示例项目,请克隆仓库并在 Example 目录下运行 pod install
。您需要 Google OAuth 客户端 ID 和 Facebook App ID。对于 Google,重命名下载的 .plist 文件为 credentials.plist 并添加到捆绑中。然后添加您的 反转 客户端 ID 作为 URL 方案。查看 https://developers.google.com/identity/sign-in/ios/start-integrating 对于 Facebook,将 Facebook App ID、显示名称 等 添加到 Info.plist 中。查看 https://developers.facebook.com/docs/facebook-login/ios#4--configure-your-project
需求
- iOS 13.0+
- Xcode 11.5+
- Swift 5.2+
安装
Socially 通过 CocoaPods 提供。要安装它,首先在你的项目目录中运行 pod init
。
• 为谷歌设置
要设置 Google Sign-In,你需要 Google OAuth 客户端 ID。请访问 此页面 创建客户端 ID。然后在 Podfile 中添加如下内容。
pod 'Socially/Google'
然后在你的项目目录中运行 pod install
。确保打开 .xcworkspace 文件。你需要将下载的 .plist 文件重命名为 credentials.plist 并添加到包中。然后添加你的 逆向 客户端 ID 作为 URL 方案。最后,在 SceneDelegate.swift 中将以下行 Google.presentingViewController(window.rootViewController)
添加到这一行 window.rootViewController = UIHostingController(rootView: contentView)
后面。
• 为 Facebook 设置
首先,访问 此页面 创建 Facebook App ID。将其添加到 Podfile。
pod 'Socially/Facebook'
然后运行 pod install
并打开 .xcworkspace 文件。然后将 Facebook App ID,显示名称 等信息添加到 Info.plist 文件。
简单使用
将 init()
添加到 SwiftUI 视图。
init() {
SociallyAuth.setGoogleProvider(afterSignIn: { (_, user, _) in
if let user = user {
print(user.profile.name ?? "N/A")
}
})
}
然后您可以使用 登录 功能进行特定的社交登录,在其内置于 SwiftUI 按钮 中。目前,社交 功能仅适用于 谷歌 和 Facebook。
Button(action: {
SociallyAuth.signIn(with: .google)
}) {
Text("Sign In with Google")
}
您可以轻松地添加 登出 功能。
Button(action: {
SociallyAuth.signOut()
}) {
Text("Sign Out")
}
应用启动后,您可以使用之前的令牌静默登录。
.onAppear {
SociallyAuth.restore()
}
作者
Phoe Lapyae
许可协议
社交软件遵循 MIT 许可协议。更多信息请参阅 LICENSE 文件。