How to properly use google signIn with SwiftUI

旧巷老猫 提交于 2021-01-04 07:47:05

问题


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()

    }
}

回答1:


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()



回答2:


So the problem was in

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

You have to present in ViewController (in my case UIRepresentableViewController worked) otherwise it will tell you that keyboard can't show or present new View

Wrapper:

struct WrapedViewController: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -> LoginViewController {
        let vc =  LoginViewController()
        print("\nmakeUIViewController \(vc)")
        return vc
    }

    func updateUIViewController(_ uiViewController: LoginViewController, context: Context) {
        print("updateUIViewController \(uiViewController)")
    }

    static func dismantleUIViewController(_ uiViewController: LoginViewController, coordinator: Self.Coordinator) {
        print("dismantleUIViewController \(uiViewController)")
    }
}

Wraped ViewController:

class LoginViewController: UIViewController {

    override func viewDidLoad() {

        let screenWidth = self.view.frame.size.width
        let screenHeight = self.view.frame.size.height

        let height: CGFloat = 40.0
        let width: CGFloat  = 120.0

        let button = UIButton(frame: CGRect(x: (screenWidth / 2.0) - (width / 2.0),
                                            y: (screenHeight / 2.0) - (height / 2.0),
                                            width: width,
                                            height: height))

        button.backgroundColor = .green
        button.setTitle("Test Button", for: .normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

        self.view.addSubview(button)
    }

    @objc func buttonAction(sender: UIButton!) {
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance()?.signIn()
    }
}


来源:https://stackoverflow.com/questions/59737264/how-to-properly-use-google-signin-with-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!