首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌ios注册移动到MainTabBarController

谷歌ios注册移动到MainTabBarController
EN

Stack Overflow用户
提问于 2019-02-22 18:05:12
回答 1查看 66关注 0票数 1

好消息是,整个注册部分工作得很好,但我似乎无法将代码从AppDelegate.swift部分移到MainTabBarController (我试图移动到的应用程序中的部分)在模拟器中,我得到的错误是,' AppDelegate‘类型的值没有成员的“解散”,每当我尝试将代码移到应用程序的另一部分到注册部分时,我就不得不将它包含在AppDelegate中。

代码语言:javascript
复制
@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?

最后注意,我正在做这个项目,只有快速代码,没有故事板部分。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-22 18:28:42

首先,您不必将它保存在AppDelegate中,您可以在任何vc中添加它,只需使用委托一致性

代码语言:javascript
复制
class LoginVC: UIViewController, GIDSignInDelegate,GIDSignInUIDelegate {
代码语言:javascript
复制
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内部进行移动,因为它是异步的

代码语言:javascript
复制
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中使用

代码语言:javascript
复制
self.window?.rootViewController = mainTab

在任何vc中使用

代码语言:javascript
复制
(UIApplication.shared.delegate as! AppDelegate).window?.rootViewController = mainTab
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54832754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档