好消息是,整个注册部分工作得很好,但我似乎无法将代码从AppDelegate.swift部分移到MainTabBarController (我试图移动到的应用程序中的部分)在模拟器中,我得到的错误是,' AppDelegate‘类型的值没有成员的“解散”,每当我尝试将代码移到应用程序的另一部分到注册部分时,我就不得不将它包含在AppDelegate中。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let err = error {
print("Failed to Login to Google", err)
return
}
print("Successfully logged into Google", user)
guard let authentication = user.authentication else { return }
guard user.authentication.idToken != nil else {return}
guard user.authentication.accessToken != nil else {return}
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
if let error = error {
print("Failed to Login Google user to Firebase db", error)
return
}
print("Successfull Login")
}
guard let mainTabBarController = UIApplication.shared.keyWindow?.rootViewController as? MainTabBarController else { return }
mainTabBarController.setupViewControllers()
self.dismiss(animated: true, completion: nil)
}
var window: UIWindow?最后注意,我正在做这个项目,只有快速代码,没有故事板部分。
发布于 2019-02-22 18:28:42
首先,您不必将它保存在AppDelegate中,您可以在任何vc中添加它,只需使用委托一致性
class LoginVC: UIViewController, GIDSignInDelegate,GIDSignInUIDelegate {override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.stream.read")
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.me")
GIDSignIn.sharedInstance().hasAuthInKeychain()
}第二,您必须在Auth.auth().signInAndRetrieveData内部进行移动,因为它是异步的
Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
if let error = error {
print("Failed to Login Google user to Firebase db", error)
return
}
print("Successfull Login")
let mainTab = mainTabBarController()
mainTab.setupViewControllers()
self.window?.rootViewController = mainTab
}在AppDelegate中使用
self.window?.rootViewController = mainTab在任何vc中使用
(UIApplication.shared.delegate as! AppDelegate).window?.rootViewController = mainTabhttps://stackoverflow.com/questions/54832754
复制相似问题