SwiftUI:在使用Facebook登录进行身份验证后导航到下一个视图

4
我正在使用SwiftUI,并在我的登录页面视图上启用了Facebook登录。当我使用Facebook或Google进行登录后,如何以编程方式导航到我的主屏幕视图?一旦我已通过任一登录进行身份验证,我希望自动将用户引导到主页。
这是我的登录视图代码:
import SwiftUI
import Firebase
import GoogleSignIn
import AuthenticationServices
import UIKit
import FBSDKLoginKit

struct LoginView: View {

    @Environment(\.window) var window: UIWindow?
    @State private var showLogin = false
    @State var appleSignInDelegates: SignInWithAppleDelegates! = nil

    var body: some View {


        VStack{

            Spacer()
            Text("Refinement")
            Spacer()

            SignInWithApple()
            .frame(width: 280, height: 60)
            .onTapGesture(perform: showAppleLogin)

            .onAppear {
              self.performExistingAccountSetupFlows()
            }
            google().frame(width: 120, height:50)


            login().frame(width: 100, height: 50)

            Spacer()
        }

    }

    struct google : UIViewRepresentable {
        func updateUIView(_ uiView: GIDSignInButton, context: UIViewRepresentableContext<LoginView.google>) {

        }

        func makeUIView(context: UIViewRepresentableContext<google>) -> GIDSignInButton {

            let button = GIDSignInButton()
            button.colorScheme = .dark
            GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
            return button
        }
    }


    func updateUIView(uiView: GIDSignInButton, context: UIViewRepresentableContext<google>) {

    }
    func gSignInMikey() {
       // GIDSignIn.sharedInstance()?.presentingViewController = ContentView

      // Automatically sign in the user.
//      GIDSignIn.sharedInstance()?.restorePreviousSignIn()

      // ...

    }
    private func showAppleLogin() {
        let request = ASAuthorizationAppleIDProvider().createRequest()
        request.requestedScopes = [.fullName, .email]

        performSignIn(using: [request])
      }

      /// Prompts the user if an existing iCloud Keychain credential or Apple ID credential is found.
      private func performExistingAccountSetupFlows() {
        #if !targetEnvironment(simulator)
        // Note that this won't do anything in the simulator.  You need to
        // be on a real device or you'll just get a failure from the call.
        let requests = [
          ASAuthorizationAppleIDProvider().createRequest(),
          ASAuthorizationPasswordProvider().createRequest()
        ]

        performSignIn(using: requests)
        #endif
      }

      private func performSignIn(using requests: [ASAuthorizationRequest]) {
        appleSignInDelegates = SignInWithAppleDelegates(window: window) { success in
          if success {
            // update UI
          } else {
            // show the user an error
          }
        }

        let controller = ASAuthorizationController(authorizationRequests: requests)
        controller.delegate = appleSignInDelegates
        controller.presentationContextProvider = appleSignInDelegates

        controller.performRequests()
      }
    }


struct login : UIViewRepresentable {
    func makeCoordinator() -> login.Coordinator {
        return login.Coordinator()
    }

    func makeUIView(context: UIViewRepresentableContext<login>) -> FBLoginButton {

        let button = FBLoginButton()
        button.delegate = context.coordinator
        return button
    }

    func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<login>) {

    }

    class Coordinator : NSObject,LoginButtonDelegate {
        func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {

            if error != nil{

                print((error?.localizedDescription)!)
                return
            }
            if AccessToken.current != nil{

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

                Auth.auth().signIn(with: credential) { (res,er) in

                    if er != nil{
                        print((er?.localizedDescription)!)
                        return

                    }
                    print("success")

                }
            }
        }

        func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
            try! Auth.auth().signOut()

        }



    }
}
1个回答

3

通过改变应用程序的状态,您可以实现手动视图更改。

使用@EnvironmentObject的示例:

class UserData: ObservableObject {
   @Published var loggedIn = false
}

在您的委托中:

let userData = UserData()
let contentView = ContentView().environmentObject(userData)

struct ContentView: View {

    @EnvironmentObject var userData: UserData

    var body: some View {
        ZStack {
            if userData.loggedIn {
                LoggedInView()
            } else {
                AuthenticationView()
            }
        }
    }
}

将userData.loggedIn更改为true将导致您的ContentView被重新绘制。

if success {
    userData.loggedIn = true
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接