【问题标题】:Google Auth with Firebase in Swift without using storyboards在 Swift 中使用 Firebase 进行 Google Auth,而不使用情节提要
【发布时间】:2021-02-08 07:47:27
【问题描述】:

我正在尝试让我的应用程序使用 Google 功能登录,而不使用 Google 的预建按钮,而是使用 GIDSignIn.sharedInstance().signIn()。但是,它想先设置视图,但我不能,因为我使用没有 Storyboards 的 SwiftUI。

我在一个名为auth() 的类中定义了一个gLog() 函数,该函数存储为authC,然后我在视图中按下按钮时调用authC.gLog(),但是它存在线程问题并在此处响应此错误:Thread 1: "presentingViewController must be set."

查看文件(SwiftUI):

//
//  ContentView.swift
//  maraki-rev1
//
//  Created by Max Webb on 25/10/20.
//

import SwiftUI
// base page name
var pageName: String = "Welcome"
var authC = auth()

struct ContentView: View {
    var body: some View {
        VStack{
            Text(verbatim: data.userFirst)
                .font(.title)
                .multilineTextAlignment(.center)
            Text("Please Log In Below")
                .padding()
            HStack{
                Button(action: {
                    authC.gLog()
                }) {
                Text("Log In with Google")
                    .padding()
            }
                Button(action: {
                    authC.aLog()
                }) {
                Text("Log In with Apple")
                    .padding()
            }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

包含glog()auth() 类的类文件。

//
//  authContent.swift
//  maraki-rev1
//
//  Created by Max Webb on 26/10/20.
//

import Foundation
import Firebase
import GoogleSignIn
import SwiftUI
class auth {
    // google auth login
    func gLog(){
        GIDSignIn.sharedInstance().signIn()

    }
    func aLog(){
        print("hello with apple")
    }
}

网上没有其他东西可以提供帮助,而且他们缺乏围绕我的问题的教程,所以我感谢所有关于这个问题的答案和 cmet。

再次感谢马克斯。

【问题讨论】:

    标签: swift firebase firebase-authentication swiftui google-signin


    【解决方案1】:

    好的,最后找到了解决这个问题的方法,发帖帮助其他人,因为这让我在过去的 3 个小时里很头疼!

    将此代码包含到我的gLog() 函数中可以让应用程序打开 Google 提示进行登录。

    if(GIDSignIn.sharedInstance()?.presentingViewController == nil){
        GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
    }
    GIDSignIn.sharedInstance()?.signIn()
    

    在这里找到这个问题的答案(包括上面的代码 sn-p):How to properly use google signIn with SwiftUI - 归功于https://stackoverflow.com/users/3961296/joshua-pogi-28

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2014-11-08
      • 2012-04-24
      • 1970-01-01
      相关资源
      最近更新 更多