Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use google signIn with SwiftUI

When I'm trying to sign in and on the next view sign out with GIDSignIn and navigate to previous view everything is fine, but when I'm trying to sign in again, the alert ask App wants to use google sign in, when I press continue - I have an error says:

Keyboard cannot present view controllers (attempted to present )

and the next error

First responder error: non-key window attempting reload - allowing due to manual keyboard (first responder window is >, key window is ; layer = >)

My code

import SwiftUI
import Foundation
import GoogleSignIn

struct LoginView: View {

    @ObservedObject var loginViewModel = LoginViewModel()

    var body: some View {

        NavigationView {
            VStack {

                Button(action: SocialLogin().attemptLoginGoogle, label: {
                    HStack{
                        Image("google")
                        Text("Google login")
                            .font(.title)
                    }
                })
                        .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
                        .background(Color.white)
                        .cornerRadius(8.0)
                        .shadow(radius: 4.0)

                NavigationLink(destination: UserData(), isActive: self.$loginViewModel.isLogedIn) {
                    EmptyView()
                }
            }
            .navigationBarTitle(Text("Login"))
        }

    }
}

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

struct SocialLogin: UIViewRepresentable {

    func makeUIView(context: UIViewRepresentableContext<SocialLogin>) -> UIView {
        return UIView()
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<SocialLogin>) {
    }

    func attemptLoginGoogle() {

        GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
        GIDSignIn.sharedInstance()?.signIn()

    }
}
like image 998
Serj Semenov Avatar asked Dec 03 '22 17:12

Serj Semenov


2 Answers

I encounter this problem in SwiftUI.

and I use this code.

      if(GIDSignIn.sharedInstance()?.presentingViewController==nil){
        GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
      }
      GIDSignIn.sharedInstance()?.signIn()
like image 196
joshua pogi 28 Avatar answered Dec 27 '22 05:12

joshua pogi 28


change

GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController

to

GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.first?.rootViewController
like image 42
Jafar Khoshtabiat Avatar answered Dec 27 '22 07:12

Jafar Khoshtabiat