如何在Google SignIn框架的App Delegate中切换@State变量?

时间:2019-11-11 20:22:16

标签: ios swift swiftui google-signin combine

我在我的SwiftUI应用的AppDelegate中使用GoogleSignIn框架。

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {

      if let error = error {
        print(error.localizedDescription)
        return
      }

      guard let authentication = user.authentication else { return }
      let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                        accessToken: authentication.accessToken)
        Auth.auth().signIn(with: credential){ (res, err) in
            if err != nil {
                print((err?.localizedDescription)!)
                return
            }
            else{
                print("User:" + (res?.user.email)!)
                // where I've tried toggling isPresentingSignIn
            }
        }
    }

SignInView工作表开始登录,该工作表在用户首次登录之前在我的Home视图上显示。登录成功后,我想撤消SignInView

struct Home: View {

    @State var isPresentingSignIn = true

    var body: some View {
        VStack {
            Text("This Is The Home View") 
        }.sheet(isPresented: $isPresentingSignIn, content: {
            SignInView()
        })
    }
}

登录效果很好,但是身份验证成功后,我不知道如何关闭SignInView。我尝试从isPresentingSignIn的登录功能中切换AppDelegate,但似乎无法访问isPresentingSignIn主体之外的Home

在成功通过身份验证后,如何解雇SignInView

1 个答案:

答案 0 :(得分:0)

@State不能设置在体外。但是ObservableObject可以。还有其他选择,但这与您尝试执行的操作最接近。