项目中没有app delegate文件时,如何在swiftui中使用google登录?

how to sign in with google in swiftui when there is no app delegate file inside the project?

我想按照URL中找到的方法登录google:

https://paulallies.medium.com/google-sign-in-swiftui-2909e01ea4ed

但问题是在swiftui新项目中找不到App delegate? 因为应用委托需要客户端 ID 实现,如 URL

所示

任何想法和谢谢

这是为所有在 AppDelegate SwiftUI 3.0 新更新中遇到问题的人准备的

在应用视图中添加

import Foundation
import SwiftUI
import Firebase
import GoogleSignIn

@main
struct YourApp: App {
    let persistenceController = PersistenceController.shared
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {

         ContentView()

            
        }
    }
}
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        
        return true
    }
    @available(iOS 9.0, *)
    func application(_ application: UIApplication, open url: URL,
                     options: [UIApplication.OpenURLOptionsKey: Any])
      -> Bool {
      return GIDSignIn.sharedInstance.handle(url)
    }
    
}

然后在loginView里面添加login struct

 @State var isLoading : Bool = false

func handleLogin(){
        // google sign in
        guard let clientID = FirebaseApp.app()?.options.clientID else { return }

        // Create Google Sign In configuration object.
        let config = GIDConfiguration(clientID: clientID)
        
        isLoading = true
        
        GIDSignIn.sharedInstance.signIn(with: config, presenting: getRootViewController()) { [self] user, error in
            if let error = error {
                isLoading = false
                print(error.localizedDescription)
              return
            }

            guard
              let authentication = user?.authentication,
              let idToken = authentication.idToken
            else {
             isLoading = false
              return
            }

            let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: authentication.accessToken)
            
            // Firebase Authentication
           Auth.auth().signIn(with: credential) {result, err in
               isLoading = false
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
               //Displaying User Name...
               guard let user = result?.user else {
                   return
               }
               print(user.displayName ?? "Success" )
                //updating user as logged in
               withAnimation{
                   log_Status = true
// and this is where you want the user to go if successful
                   goHome()
               }
            }
            
    }
  
    
}

goHome() 函数在哪里

public func goHome() {
    if let window = UIApplication.shared.windows.first {
        window.rootViewController = UIHostingController(rootView: HomeView())
        window.makeKeyAndVisible()
    }
}

也将其添加到 loginView 但作为扩展

extension View{
    func getRect()->CGRect{
        return UIScreen.main.bounds
    }
        // retrieve RootView
    func getRootViewController()->UIViewController{
        guard let screen = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
            return . init()
            
        }
        
        guard let root = screen.windows.first?.rootViewController else {
            return .init()
        }
        return root
    }
}

别忘了给按钮 handleLogin()

   Button(action: {handleLogin()}) {
                            Image("Gmail")
                        }
                        .padding()
                        .foregroundColor(.white)
                        .background(Color.white)
                        .cornerRadius(.infinity)

你很高兴去我的朋友们