swift – Fb iOS SDK login callback not working

swift – Fb iOS SDK login callback not working


I am making an attempt to implement Fb login through iOS SDK

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

The callback perform 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

@most important
struct myapp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var physique: some Scene {
        WindowGroup {
            FacebookLoginView()
                .body(width: 200, top: 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 outcome: LoginManagerLoginResult?,
            error: Error?
        ) {
            print("callback triggered")
            if outcome?.token != nil {
                print("Logged in")
            } else {
                print("Didn't login")
            }
            
            if let error = error {
                print("Login failed with error: (error.localizedDescription)")
                return
            }

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

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

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

My Information.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

Replace:

I attempted including this SceneDelegate nevertheless it did not have an effect on something:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(
        _ scene: UIScene,
        openURLContexts URLContexts: Set
    ) {
        print("scene delegate openURLContexts known as")
        guard let url = URLContexts.first?.url else {
            return
        }

        ApplicationDelegate.shared.software(
            UIApplication.shared,
            open: url,
            sourceApplication: nil,
            annotation: [UIApplication.OpenURLOptionsKey.annotation]
        )
    }

    func scene(
        _ scene: UIScene,
        willConnectTo session: UISceneSession,
        choices connectionOptions: UIScene.ConnectionOptions
    ) {
        guard let windowScene = scene as? UIWindowScene else { return }
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: FacebookLoginView())
        self.window = window
        window.makeKeyAndVisible()
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *