SwiftUI Facebook 登录:如何在视图中获取 Facebook id?
Posted
技术标签:
【中文标题】SwiftUI Facebook 登录:如何在视图中获取 Facebook id?【英文标题】:SwiftUI Facebook Login: How to get the Facebook id back in the View? 【发布时间】:2021-01-18 20:14:58 【问题描述】:我正在使用How to login to Facebook on SwiftUI? 中的以下代码在我的应用程序中使用 Facebook 登录。效果很好,除了我找不到将 id 和访问令牌取回我的视图的方法,我需要在其中更新变量。
我的代码:
LoginView.swift
import SwiftUI
// Facebook Login
import FBSDKLoginKit
import FacebookLogin
[...]
// Facebook Login
let facebookLoginButton = FBLoginButton(frame: .zero, permissions: [.publicProfile])
struct LoginView: View
@EnvironmentObject var viewRouter: ViewRouter
// Facebook Login
@ObservedObject var fbmanager = UserLoginManager()
var body: some View
[...]
Button(action:
self.fbmanager.facebookLogin()
)
FacebookButtonContent(buttonWidth: 120, buttonHeight: 60, buttonRadius: 15)
[...]
[...]
// Facebook Login
class UserLoginManager: ObservableObject
let loginManager = LoginManager()
func facebookLogin()
loginManager.logIn(permissions: [.publicProfile, .email], viewController: nil) loginResult in
switch loginResult
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
print("Logged in! \(grantedPermissions) \(declinedPermissions) \(accessToken)")
GraphRequest(graphpath: "me", parameters: ["fields": "id, name, first_name"]).start(completionHandler: (connection, result, error) -> Void in
if (error == nil)
let fbDetails = result as! NSDictionary
print(fbDetails)
let fbUserId = fbDetails["id"] ?? ""
let fbAccessToken = AccessToken.current?.tokenString ?? ""
)
//
// QUESTION: how to get the fbUserId and fbAccessToken here ???
//
这是我卡住的地方:我想在 GraphRequest 调用之后获取 fbUserId 和 fbAccessToken,以便我的函数 facebookLogin 可以返回它们。我该怎么做?
感谢您的帮助!
【问题讨论】:
【参考方案1】:我找到了如何做到这一点,在这里发布解决方案:
我将facebookLogin函数修改如下:
facebookLogin(_ completion: @escaping (String) -> ())
完整的UserLoginManager类代码:
// Facebook Login
class UserLoginManager: ObservableObject
let loginManager = LoginManager()
func facebookLogin(_ completion: @escaping (String) -> ())
loginManager.logIn(permissions: [.publicProfile, .email], viewController: nil) loginResult in
switch loginResult
case .failed(let error):
print(error)
completion("")
case .cancelled:
print("User cancelled login.")
completion("")
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
print("Logged in! \(grantedPermissions) \(declinedPermissions) \(accessToken)")
GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name"]).start(completionHandler: (connection, result, error) -> Void in
if (error == nil)
let fbDetails = result as! NSDictionary
print(fbDetails)
let fbUserId = fbDetails["id"] ?? ""
let fbAccessToken = AccessToken.current?.tokenString ?? ""
let fbResult = "id=\(fbUserId)&accessToken=\(fbAccessToken)"
completion(fbResult)
else
completion("")
)
然后在我的按钮中我做了以下操作:
Button(action:
self.fbmanager.facebookLogin (completionData) in
// Do operation with String
print(completionData);
)
FacebookButtonContent(buttonWidth: 120, buttonHeight: 60, buttonRadius: 15)
希望这可以帮助其他有同样问题的人...
【讨论】:
以上是关于SwiftUI Facebook 登录:如何在视图中获取 Facebook id?的主要内容,如果未能解决你的问题,请参考以下文章
Facebook 登录 + SwiftUI:登录/注销后不调用 loginButton 和 loginButtonDidLogOut
安装了 FB 应用程序的 SwiftUI 自定义 Facebook 登录