Ser*_*nov 4 ios swift google-signin swiftui
当我尝试登录并在下一个视图中使用 GIDSignIn 注销并导航到上一个视图时一切正常,但是当我再次尝试登录时,警报询问App 想要使用 google 登录,当我按继续 - 我有一个错误说:
键盘无法呈现视图控制器(试图呈现)
和下一个错误
第一响应者错误:尝试重新加载非关键窗口 - 由于手动键盘而允许(第一响应者窗口是 >,关键窗口是;层 = >)
我的代码
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()
}
}
Run Code Online (Sandbox Code Playgroud)
我在 SwiftUI 中遇到了这个问题。
我使用这个代码。
if(GIDSignIn.sharedInstance()?.presentingViewController==nil){
GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
}
GIDSignIn.sharedInstance()?.signIn()
Run Code Online (Sandbox Code Playgroud)
改变
GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
Run Code Online (Sandbox Code Playgroud)
到
GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.first?.rootViewController
Run Code Online (Sandbox Code Playgroud)
所以问题出在
GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
Run Code Online (Sandbox Code Playgroud)
您必须在 ViewController 中呈现(在我的情况下 UIRepresentableViewController 有效),否则它会告诉您键盘无法显示或呈现新视图
包装:
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)")
}
}
Run Code Online (Sandbox Code Playgroud)
包装的视图控制器:
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()
}
}
Run Code Online (Sandbox Code Playgroud)