注销时清除控制器

时间:2020-07-30 16:25:41

标签: ios swift firebase authentication controller

我在iOS应用中创建一个注销按钮,但是当您注销并使用新帐户登录时,应用stil会显示旧数据。

这是我的注销处理程序:

@objc func handleLogout(){
        
        do{
            try Auth.auth().signOut()
            //UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
        } catch let logoutError {
            print(logoutError)
        }
        
        DispatchQueue.main.async {
            // UIView usage
            let loginController = LoginController()
            loginController.modalPresentationStyle = .fullScreen
            
            self.present(loginController, animated: true, completion: nil)
        }
        
    }

用户注销时如何重设所有内容?

2 个答案:

答案 0 :(得分:0)

即使Auth出错,您当前的代码仍将假定注销,因此

@objc func handleLogout() {  
      do {
            try Auth.auth().signOut()  
            let loginController = LoginController()
            loginController.modalPresentationStyle = .fullScreen 
            self.present(loginController, animated: true, completion: nil) 
        } catch {
            print(error)
      } 
}

还应该替换此行

self.present(loginController, animated: true, completion: nil) 

使用window的rootViewController进行更改,因为它将使内存泄漏而使先前的vcs保持活动状态,因此将其更改为

(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = loginController 

答案 1 :(得分:0)

登录后,您将返回到UIViewController的现有实例。登出成功后,您应该展示以前的UIViewController的新实例。注销成功后,尝试将rootViewController设置为LoginViewController

@objc func handleLogout(){
    do{
        try Auth.auth().signOut()
        DispatchQueue.main.async {
            UIApplication.shared.keyWindow?.rootViewController = LoginController()
        }
    } catch let logoutError {
        print(logoutError)
    }
}