如何解决如果我注销并登录然后第一次无法登录,为什么?迅速
我的应用流程如下
uinavigationcontroller(is initial viewcontroller) -> loginVC -> homeVC
我在项目中有 SceneDelegate,所以尝试像下面这样的代码
场景委托代码:
func scene(_ scene: UIScene,willConnectTo session: UIScenesession,options connectionoptions: UIScene.Connectionoptions) {
let userLoginStatus = UserDefaults.standard.bool(forKey: "USER_LOGIN")
if (userLoginStatus) {
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
let vc_TabBar = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
window!.rootViewController = vc_TabBar
window!.makeKeyAndVisible()
}
guard let _ = (scene as? UIWindowScene) else { return }
}
@IBAction func loginBtnTapped(_ sender: Any) {
guard let email = emailTF.text,let password = passwordTF.text
else {
print("form is not valis")
return
}
// UserDefaults.standard.set(true,forKey: "USER_LOGIN")
Auth.auth().signIn(withEmail: email,password: password) { (result,error) in
if let _eror = error{
print(_eror.localizedDescription)
}else{
if let _res = result{
print(_res)
UserDefaults.standard.set(true,forKey: "USER_LOGIN")
let vc = UIStoryboard.init(name: "Main",bundle: Bundle.main).instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
self.navigationController?.pushViewController(vc!,animated: true)
}
}
}
}
homveVC 注销按钮代码:
@IBAction func logoutBtnTapped(_ sender: Any) {
UserDefaults.standard.set(false,forKey: "USER_LOGIN") //logging session off
do{
try Auth.auth().signOut()
}catch let logoutError{
print(logoutError)
}
// UserDefaults.standard.set(false,forKey: "USER_LOGIN") //logging session off
let vc = UIStoryboard.init(name: "Main",bundle: Bundle.main).instantiateViewController(withIdentifier: "SignInViewController") as? SignInViewController
self.navigationController?.pushViewController(vc!,animated: true)
}
使用上面的代码我可以自动登录,但是一旦我注销,如果我登录,它就不会转到 homeVC .. 如果我停止并运行然后显示 homeVC 为什么,我错在哪里.. 请提供帮助。
解决方法
我会按如下方式更改登录名:
删除 SceneDelegate 中的所有内容:
func scene(_ scene: UIScene,willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
}
您的 SignInViewController
更改如下:
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
userIsloggedin()
}
func userIsloggedin(){
let userLoginStatus = UserDefaults.standard.bool(forKey: "USER_LOGIN")
if (userLoginStatus) {
self.performSegue(withIdentifier: "toHomeVC",sender: self)
}
}
@IBAction func loginBtnTapped(_ sender: UIButton) {
UserDefaults.standard.set(true,forKey: "USER_LOGIN")
self.performSegue(withIdentifier: "toHomeVC",sender: self)
}
在您的故事板中创建一个名为“toHomeVC”的转场。您的故事板应如下所示:
然后在 HomeViewController
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true //to hide the back button
}
@IBAction func logoutBtnTapped(_ sender: UIButton) {
UserDefaults.standard.set(false,forKey: "USER_LOGIN") //logging session off
self.navigationController?.popToRootViewController(animated: true) // or false if you don't want to see the animation
}
最后,如果您需要使用 UITabarController,只需像这样嵌入您的 HomeVc:
你的故事板应该是这样的:
如果您决定将其嵌入 UITabBarController,您必须像这样隐藏后退按钮:
self.tabBarController?.navigationItem.hidesBackButton = true
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。