Facebook 登录 + SwiftUI:登录/注销后不调用 loginButton 和 loginButtonDidLogOut

Posted

技术标签:

【中文标题】Facebook 登录 + SwiftUI:登录/注销后不调用 loginButton 和 loginButtonDidLogOut【英文标题】:Facebook Login + SwiftUI: loginButton & loginButtonDidLogOut are NOT called after login/logout 【发布时间】:2020-06-17 12:20:55 【问题描述】:

我正在尝试为我的 SwiftUI 应用实现 Facebook 登录,这是我的 AppDelegate.swift 文件:

import UIKit
import FirebaseCore
import FirebaseAuth
import FBSDKLoginKit
import FBSDKCoreKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, LoginButtonDelegate 

    static var orientationLock = UIInterfaceOrientationMask.portrait

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask 
        return AppDelegate.orientationLock
    

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 
        // Override point for customization after application launch.
        FirebaseApp.configure()

        return ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
    

    @available(ios 9.0, *)
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any])
        -> Bool 

            let handled = ApplicationDelegate.shared.application(
                application,
                open: url,
                sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
                annotation: options[UIApplication.OpenURLOptionsKey.annotation]
            )

            return handled

    

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool 

        return ApplicationDelegate.shared.application(
            application,
            open: url
        )
    

    func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) 
        print("FB Login Button function")
        if let error = error 
            print("ERROR1: \(error.localizedDescription)")
            return
        

        let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)

        Auth.auth().signIn(with: credential)  (res, err) in
            if err != nil 
                print("ERROR2: \(String(describing: err?.localizedDescription))")
                return
            

            print("email: \(String(describing: res?.user.email))")
            print("name: \(String(describing: res?.user.displayName))")
        

    

    func loginButtonDidLogOut(_ loginButton: FBLoginButton) 
        print("Did logout")
    

好的,我的 SwiftUI 代码来了:

import SwiftUI
import FirebaseCore
import FBSDKLoginKit

struct LoginView: View 
    @EnvironmentObject var thisSession: CurrentSession
    @ObservedObject var mainData = MainViewModel()

    var body: some View 
        VStack 
            facebook().frame(width: 240, height: 50)
        
    


struct LoginView_Previews: PreviewProvider 
    static var previews: some View 
        LoginView().environmentObject(CurrentSession())
    


struct facebook : UIViewRepresentable 

    func makeUIView(context: UIViewRepresentableContext<facebook>) -> FBLoginButton 

        let button = FBLoginButton()
        //button.delegate = self
        return button
    
    func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<facebook>) 
        print("FBButton updateUIView called")
    

我可以在我的 LoginView 上看到按钮,我什至可以登录/注销(按钮上的文本分别从“登录”更改为“注销”... 但是来自“登录按钮”和“ loginButtonDidLogOut" 永远不会被执行。我应该在我的调试控制台中看到一些消息,但是这段代码没有被调用。

我做错了吗?好像我忘了设置一些委托,或者我应该在其他地方调用 Auth.auth() 而不是 loginButton() 函数..

【问题讨论】:

你解决了吗,我也有同样的问题! 【参考方案1】:

你的AppDelegate符合LoginButtonDelegate,所以你只需要在makeUIView内设置委托:

func makeUIView(context: UIViewRepresentableContext<facebook>) -> FBLoginButton 
    let button = FBLoginButton()
    button.delegate = UIApplication.shared.delegate as! AppDelegate
    return button

【讨论】:

以上是关于Facebook 登录 + SwiftUI:登录/注销后不调用 loginButton 和 loginButtonDidLogOut的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI Facebook 登录:如何在视图中获取 Facebook id?

安装了 FB 应用程序的 SwiftUI 自定义 Facebook 登录

使用 SwiftUI 成功登录后导航

纯SwiftUI的登录、注册、注册流程,可以吗?

在 SwiftUI 中调出 Google 登录屏幕

登录后如何在 SwiftUI 中显示新视图