如何在SwiftUI上登录Facebook? [英] How to login to Facebook on SwiftUI?

查看:123
本文介绍了如何在SwiftUI上登录Facebook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有太多资源可以解释使用SwiftUI进行Facebook登录.我不确定我的代码是否需要 ViewController ,因为Facebook的 LoginManager.login()包含 ViewController 参数-但这不是.真的可以翻译成SwiftUI.

There are not many resources explaining Facebook Login with SwiftUI. I'm not sure whether my code requires a ViewController or not because Facebook's LoginManager.login() contains a ViewController parameter - however this doesn't really translate to SwiftUI.

无论如何,当用户单击下面的 Button 时,我试图将其登录到Facebook:

Regardless, I am trying to login the user to Facebook when they click on the Button below:

LoginView.swift

import Foundation
import SwiftUI
import FBSDKLoginKit

struct LoginView: View {
    @EnvironmentObject var auth: UserAuth
    var body: some View {
        ZStack {
            VStack {
                Image("launcher_logo").resizable()
                .scaledToFit()
                .frame(height: 100)
                    .padding(.top, 100)
                Spacer()
                Button(action: {
                    FBLogin()
                }) {
                    Text("Continue with Facebook")
                }.foregroundColor(Color.black)

当点击 Button 时,它会初始化下面的 FBLogin -在其 init()中触发 login()代码>:

when the Button is clicked, it initialises FBLogin below - which fires login() in its init():

型号:

class FBLogin: LoginManager {

    let loginButton = FBLoginButton()
    let token = AccessToken.current
    let permissions = ["user_birthday", "user_gender", "public_profile"]

    override init(){
        super.init()
        logIn(permissions: permissions, from: nil)
        print("fb init()")
    }

    override func logIn(permissions: [String], from fromViewController: UIViewController?, handler: LoginManagerLoginResultBlock? = nil) {
        // TODO
    }

}

但是我不确定从那里做什么.目前,仅打印 fb init(),但我想执行登录并收听登录结果.

However I'm not sure what to do from there. At the moment, only fb init() prints but I want to execute the login and listen to the login result.

有什么主意吗?

推荐答案

只需在 ObservableObject 内创建 LoginManager 实例,然后自定义登录完成.您可以在 SwiftUI视图内部轻松使用.不需要 UIViewControllerRepresentable .这是我的样品.

Just create LoginManager instance inside ObservableObject and then customise login completion. You can easily use inside SwiftUI View. No need UIViewControllerRepresentable. Here is my sample.

struct ContentView: View {
    @ObservedObject var fbmanager = UserLoginManager()
    var body: some View {
        Button(action: {
            self.fbmanager.facebookLogin()
        }) {
            Text("Continue with Facebook")
        }
    }
}

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)
                    }
                })
            }
        }
    }
}

这篇关于如何在SwiftUI上登录Facebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆