Categories
facebook facebook-login ios swift swiftui

How to login to Facebook on 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.

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)

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

Model:

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

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.

Any idea?