我目前正在为我的应用程序开发 GoogleSignIn 实现,并且 func 抛出错误
"Type 'Application_utility' has no member 'rootViewController'"
,我不知道如何修复它。
这是引发错误的代码:
// MARK: SignIn method
GIDSignIn.sharedInstance.signIn(withPresenting: Application_utility.rootViewController) { user, error in
// some code
}
这是我的Application_utility:
import SwiftUI
import UIKit
final class Application_utility: UIViewController {
static var UIViewController: 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
}
}
如果有人可以解释这个问题以及如何解决它,我将非常感激。
我必须将 `return root` 更改为 `return rootViewController` 吗?
GIDSignIn.sharedInstance.signIn
将输入参数作为 UIViewController 来呈现登录表单,因此您需要提供当前呈现控制器。或者在这种情况下,我想您将有一个返回当前控制器的静态属性
final class ApplicationUtility: UIViewController? {
static var rootViewController: UIViewController? {
let keyWindow = UIApplication.shared.windows.filter { $0.isKeyWindow }.first
if var topController = keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
return topController
}
return nil
}
}