Google登出并返回登录页面

时间:2019-04-26 06:46:56

标签: swift google-signin

我想单击Sign Out上的ListViewController按钮以注销用户,然后返回到SignInViewController,如下图所示。

enter image description here

ListViewController中的SignOut按钮:

@IBAction func didTapSignOut(_ sender: Any) {
    //Sign Out
    GIDSignIn.sharedInstance().signOut()
    //Go to the `SignInViewController`
    let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let desVC = mainStoryboard.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
    self.navigationController?.pushViewController(desVC, animated: true)
}

返回SignInViewController时,有一个back按钮,该按钮直接返回到其上的ListViewController。该应用似乎仍然具有用户数据的缓存,因此该用户实际上并未注销。

enter image description here

但是我想要回到用户必须再次登录的应用的初始状态。

我的故事板enter image description here

我如何从SignInViewControllerListViewControllerAppDelegate

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
    if let error = error {
        print("\(error.localizedDescription)")
    } else {
      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabbarIdentifier") as! UITabBarController
      self.window?.rootViewController?.present(tabbarVC, animated: false, completion: nil)
    }
}

我尝试过的解决方案

var window: UIWindow?
@IBAction func didTapSignOut(_ sender: Any) {
    GIDSignIn.sharedInstance().signOut()

    let desVC: UIViewController = SignInViewController()
    if let window = self.window{
        window.rootViewController = desVC
    }
    self.navigationController?.popToRootViewController(animated: true)
}

但是现在单击按钮后视图没有改变。

2 个答案:

答案 0 :(得分:1)

在google登录中,您将从signinViewcontroller中显示标签栏控制器。因此,只需在点击“注销”按钮时关闭标签栏控制器

@IBAction func didTapSignOut(_ sender: Any) {
    //Sign Out
    GIDSignIn.sharedInstance().signOut()
    //Go to the `SignInViewController`
    self.tabBarController?.dismiss(animated: true, completion: nil)  
}

答案 1 :(得分:0)

self.navigationController?.pushViewController更改为self.navigationController?.popToRootViewController(这将弹出动画)

如果需要推送动画,请使用self.navigationController?.setViewControllers([desVC])

更新1:

如果您不介意没有推送/弹出动画,则可以直接更改窗口

let nvc = UINavigationController(rootViewController: desVC)
let window = UIApplication.shared.window
window.rootViewController = nvc

更新2:

是的,通过navigationController进行更新在您的层次结构中不合适。只需更改窗口的rootViewController(如更新1中所述)