FirebaseUI和SwiftUI LoginG

时间:2019-10-12 10:36:15

标签: ios swift authentication swiftui firebaseui

我正在尝试使用swiftUI在iOS应用中实现firebaseUI登录。

我可以使用主登录视图,但是在用户登录后我无法控制流程。

这是我的“登录控制器”的代码

import SwiftUI
import FirebaseUI

struct CustomLogin: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UIViewController {
        let authUI = FUIAuth.defaultAuthUI()
        let providers: [FUIAuthProvider] = [
          FUIEmailAuth(),
          FUIFacebookAuth()

         // FUIPhoneAuth(authUI:FUIAuth.defaultAuthUI()),
        ]
        authUI?.providers = providers
        let authViewController = authUI?.authViewController()
      //  let controller = UIViewController()

     //   authUI!.delegate = self
        return authViewController!

       }

       func updateUIViewController(_ uiViewController: UIViewController, context: Context) {


    }

}

struct CustomLogin_Previews: PreviewProvider {
    static var previews: some View {
        CustomLogin()
    }
}

我可以通过将CustomLogin())添加到内容视图中来显示登录流程。

用户登录后如何管理回叫?

enter image description here

用户是在firebase中创建的,但是视图不会更新。

如果有人使用SwiftUI实现了firebaseUI登录,则表示感谢。

谢谢

1 个答案:

答案 0 :(得分:0)

我能够根据您的代码和一些Apple教程来实现某些有效的方法。基本上,您需要在UIViewControllerRepresentable中添加协调器。您可以将您的代表添加到协调器中。

这是我有效的草稿代码:

import SwiftUI
import FirebaseUI
import Firebase

public var screenWidth: CGFloat {
    return UIScreen.main.bounds.width
}

public var screenHeight: CGFloat {
    return UIScreen.main.bounds.height
}

struct LoginView : View {

    @State private var viewState = CGSize(width: 0, height: screenHeight)
    @State private var MainviewState = CGSize.zero

    var body : some View {
        ZStack {
            CustomLoginViewController { (error) in
                if error == nil {
                    self.status()
                }
            }.offset(y: self.MainviewState.height).animation(.spring())
            MainView().environmentObject(DataStore()).offset(y: self.viewState.height).animation(.spring())
        }
    }

    func status() {
        self.viewState = CGSize(width: 0, height: 0)
        self.MainviewState = CGSize(width: 0, height: screenHeight)
    }
}

struct LoginView_Previews : PreviewProvider {
    static var previews : some View {
        LoginView()
    }
}

struct CustomLoginViewController : UIViewControllerRepresentable {

    var dismiss : (_ error : Error? ) -> Void

    func makeCoordinator() -> CustomLoginViewController.Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> UIViewController
    {
        let authUI = FUIAuth.defaultAuthUI()

        let providers : [FUIAuthProvider] = [
            FUIEmailAuth(),
            FUIGoogleAuth(),
            FUIOAuth.appleAuthProvider()
        ]

        authUI?.providers = providers
        authUI?.delegate = context.coordinator

        let authViewController = authUI?.authViewController()

        return authViewController!
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<CustomLoginViewController>)
    {

    }

    //coordinator
    class Coordinator : NSObject, FUIAuthDelegate {
        var parent : CustomLoginViewController

        init(_ customLoginViewController : CustomLoginViewController) {
            self.parent = customLoginViewController
        }

        // MARK: FUIAuthDelegate
        func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?)
        {
            if let error = error {
                parent.dismiss(error)
            }
            else {
                parent.dismiss(nil)
            }
        }

        func authUI(_ authUI: FUIAuth, didFinish operation: FUIAccountSettingsOperationType, error: Error?)
        {
        }
    }
}