如何通过SwiftUI正确使用Google SignIn [英] How to properly use google signIn with SwiftUI

查看:151
本文介绍了如何通过SwiftUI正确使用Google SignIn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试登录并在下一个视图上使用GIDSignIn注销并导航至上一个视图时,一切都很好,但是当我尝试再次登录时,警告询问 App想要使用google登录,当我按继续-出现错误提示:

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:

键盘无法显示视图控制器(试图显示)

和下一个错误

第一响应者错误:尝试重新加载非键窗口-由于手动键盘允许(第一响应器窗口为>,键窗口为; layer =>)

我的代码

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

    }
}

推荐答案

所以问题出在

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

您必须在ViewController中显示(在我的情况下,UIRepresentableViewController工作正常),否则它将告诉您键盘无法显示或显示新的View

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

包装器:

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)")
    }
}

包装的ViewController:

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

这篇关于如何通过SwiftUI正确使用Google SignIn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆