Search code examples
facebookswiftuifacebook-login

SwiftUI Facebook login popup wont close?


i am trying to implement facebook login button into my project , all good but the dialog once return from Facebook still open and not closing . i think its something related to URL app delegate its not firing since i am using SwiftUI without scenedelegate .

The login func which is work perfectly :

fileprivate func facebookLogin(){
    
    let fbLoginManager = LoginManager()
        fbLoginManager.logIn(permissions: ["public_profile", "email"], from: Tools.topViewController()! )
        {  (result, error) in
            
            if ( result != nil && (result?.isCancelled)! )
            {
                print("[LOGIN][FACEBOOK] cancecled by User")
                return
            }
            
            if  result != nil && (result?.grantedPermissions != nil)
            {
                print("[LOGIN][FACEBOOK] grantedPermissions")
                
                if((AccessToken.current) != nil)
                {
                    GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in
                        
                        if (error == nil)
                        {
                            
                            print("[LOGIN][FACEBOOK SUCCESS] \(String(describing: result))")
                            
                            var fb_result = result as! [String : Any]
                            
                            let name = fb_result["name"] as! String
               
                        }else
                        {
                            print("[LOGIN][FACEBOOK ERROR] \(String(describing: error))")
                        }
                        
                    })
                    
                }else
                //Something went Wrong !
                {
                    print("[LOGIN][FACEBOOK ERROR] Something went wrong! #1")
                }
                
            }
            //Something went Wrong !
            else{
                
            }
            
        }
}

App :

@main
struct LW_APP: App {

    @Environment(\.scenePhase) var scenePhase

   
    
    var body: some Scene {
        WindowGroup {
            
            w_landingTabs()
      
        }
        .onChange(of: scenePhase) { (newScenePhase) in
            switch newScenePhase {
            case .background:
                print("[APP] State : Background")
            case .inactive:
                print("[APP] State : Inactive")
            case .active:
                print("[APP] State : Active")
           
            @unknown default:
                print("[APP] State : Unknown")
            }
        }
    }
    
}

App delegate :

import UIKit
import FBSDKCoreKit

let APP_DELEGATE:APP_delegate = UIApplication.shared.delegate as! APP_delegate

class APP_delegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        ApplicationDelegate.shared.application(
                  application,
                  didFinishLaunchingWithOptions: launchOptions
              )
        
        print("[APP DELEGATE] MyAppDelegate didFinishLaunchingWithOptions called")
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        print("[APP DELEGATE] options: [UIApplication.OpenURLOptionsKey : Any] ")
            return ApplicationDelegate.shared.application(app, open: url, options: options)
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        print("[APP DELEGATE] applicationDidBecomeActive")
        AppEvents.activateApp()
    }
  
}

The screen i stuck on when return after login :

enter image description here


Solution

  • I've found the only solution for this issue.

    This part of code :

    let _ = ApplicationDelegate.shared.application(
                                UIApplication.shared,
                                open: url,
                                sourceApplication: nil,
                                annotation: [UIApplication.OpenURLOptionsKey.annotation])
    

    must be added inside onOpenURL for SwiftUI view , in my case i did this :

    @main
    struct LW_APP: App {
        
        @UIApplicationDelegateAdaptor(APP_delegate.self) private var appDelegate
        @Environment(\.scenePhase) var scenePhase
    
            var body: some Scene {
                WindowGroup {
                    
                    if Env.sharedInstance.openApp {
                        w_landingTabs()
                            .environmentObject(env)
                            .onOpenURL { (url) in
                         
                                let _ = ApplicationDelegate.shared.application(
                                    UIApplication.shared,
                                    open: url,
                                    sourceApplication: nil,
                                    annotation: [UIApplication.OpenURLOptionsKey.annotation])
                                
                        }
        .....