11.8 C
New York
Tuesday, November 5, 2024

swift – Fb iOS SDK login callback not working


I am attempting to implement Fb login through iOS SDK

Following this tutorial: https://builders.fb.com/docs/facebook-login/ios

The callback operate by no means will get known as on success, it solely will get known as when the person cancels.

The package deal dependency is Fb v14.1.0
iOS model 17.6

import SwiftUI
import FBSDKLoginKit

@essential
struct myapp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var physique: some Scene {
        WindowGroup {
            FacebookLoginView()
                .body(width: 200, peak: 50)
        }
    }
}

struct FacebookLoginView: UIViewRepresentable {
    typealias UIViewType = FBLoginButton

    func makeUIView(context: Context) -> UIViewType {
        let button = FBLoginButton()
        button.permissions = ["public_profile", "email"]
        button.delegate = context.coordinator
        return button
    }

    func updateUIView(_ uiView: FBLoginButton, context: Context) { }

    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }

    class Coordinator: NSObject, LoginButtonDelegate {
        func loginButton(
            _ loginButton: FBLoginButton,
            didCompleteWith end result: LoginManagerLoginResult?,
            error: Error?
        ) {
            print("callback triggered")
            if end result?.token != nil {
                print("Logged in")
            } else {
                print("Didn't login")
            }
            
            if let error = error {
                print("Login failed with error: (error.localizedDescription)")
                return
            }

            if let end result = end result, !end result.isCancelled {
                if let token = end result.token?.tokenString {
                    print("Entry Token: (token)")
                }
            }
        }

        func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
            print("Consumer logged out")
        }
    }
}

class AppDelegate: UIResponder, UIApplicationDelegate {
    func utility(
        _ utility: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        ApplicationDelegate.shared.utility(
            utility,
            didFinishLaunchingWithOptions: launchOptions
        )
        
        return true
    }
    
    func utility(
        _ app: UIApplication,
        open url: URL,
        choices: [UIApplication.OpenURLOptionsKey : Any] = [:]
    ) -> Bool {
        ApplicationDelegate.shared.utility(
            app,
            open: url,
            sourceApplication: choices[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
            annotation: choices[UIApplication.OpenURLOptionsKey.annotation]
        )
    }
}

My Data.plist





    CFBundleURLTypes
    
        
            CFBundleURLSchemes
            
                fb
            
        
    
    FacebookAppID
    
    FacebookClientToken
    
    FacebookDisplayName
    
    LSApplicationQueriesSchemes
    
        fbapi
        fb-messenger-share-api
    
  LSApplicationQueriesSchemes

    fbapi
    fb-messenger-share-api
    fbauth2
    fbshareextension




I can see the fb login button.
I can click on on the button.
It redirects to the Fb app and asks me to “Proceed as …”
Lastly, it redirects again to the sheet/webview after clicking Proceed however the callback doesn’t get known as.

step 1

step 2

step 3

step 4

step 5

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles