我是Swift的新手。我正在开发应用程序。我正在使用Firebase将用户注册并登录到我的应用程序。 这是我的登录代码:-
@IBAction func onClickLogin(_ sender: Any)
{
let email = Validation.shareValidator.isValidEmail(email: txtEmail.text, view: viewContainerEmail)
let password = Validation.shareValidator.isValidPassword(password: txtPassword.text, view: viewContainerPassword)
if email != "success"
{
CustomAlert.shareAlert.alertSetUp(title: "Email Field", subTitle: email, boldTitle: "Invalid Email")
}
else if password != "success"
{
CustomAlert.shareAlert.alertSetUp(title: "Password Field", subTitle: password, boldTitle: "Invalid Password")
}
else
{
Auth.auth().signIn(withEmail: txtEmail.text!, password: txtPassword.text!) { (result, error) in
if error != nil
{
CustomAlert.shareAlert.alertSetUp(title: "Login Error", subTitle: error!.localizedDescription, boldTitle: "Login Error")
}
else
{
self.btnLogin.startAnimate(spinnerType: SpinnerType.circleStrokeSpin, spinnercolor: UIColor.white, spinnerSize: 20, complete: {
self.btnLogin.backgroundColor = UIColor.green
let defaults = UserDefaults.standard
defaults.set(true, forKey: "isLogin")
defaults.set(result?.user.uid, forKey: "uid")
defaults.set(self.txtEmail.text!, forKey: "email")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "RootTabBarC") as! RootTabBarC
self.navigationController?.pushViewController(vc, animated: true)
})
}
}
}
}
这是我的注册代码:-
@IBAction func onClickSignUp(_ sender: Any)
{
Auth.auth().createUser(withEmail: txtEmail.text!, password: txtPassword.text!) { (result, error) in
if error != nil
{
CustomAlert.shareAlert.alertSetUp(title: "Registration Error", subTitle: error!.localizedDescription, boldTitle: "Registration Error")
}
else
{
let uid = result?.user.uid
let ref = Database.database().reference(withPath: "user").child(uid!)
ref.setValue(["name" : self.txtName.text!, "email" : self.txtEmail.text!, "password" : self.txtPassword.text!, "mobile" : "", "recipeExpert" : "", "bio" : "", "profileImage" : ""])
self.btnSignUp.startAnimate(spinnerType: SpinnerType.circleStrokeSpin, spinnercolor: UIColor.white, spinnerSize: 20, complete: {
self.btnSignUp.backgroundColor = UIColor.green
let defaults = UserDefaults.standard
defaults.set(true, forKey: "isLogin")
defaults.set(result?.user.uid, forKey: "uid")
defaults.set(self.txtEmail.text!, forKey: "email")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "RootTabBarC") as! RootTabBarC
self.navigationController?.pushViewController(vc, animated: true)
})
}
}
这是我的注销代码:-
@IBAction func onClickLogOut(_ sender: Any)
{
do
{
try Auth.auth().signOut()
UserDefaults.standard.removeObject(forKey: "isLogin")
UserDefaults.standard.removeObject(forKey: "uid")
UserDefaults.standard.removeObject(forKey: "email")
UserDefaults.standard.synchronize()
}
catch let err
{
print(err.localizedDescription)
}
let appDel = UIApplication.shared.delegate as! AppDelegate
let vc = storyboard?.instantiateViewController(identifier: "LoginSignUpVC") as? LoginSignUpVC
let navVc = UINavigationController(rootViewController: vc!)
appDel.window?.rootViewController = navVc
appDel.window?.makeKeyAndVisible()
self.parent?.navigationController?.popToRootViewController(animated: true)
}
当我运行我的应用程序时,它可以正常运行,但是当我关闭我的应用程序在模拟器上的“最近使用”标签,然后再次打开并尝试注销时,它不会返回我的 LoginSignUpVc >根视图控制器。 self.parent?.navigationController?.popToRootViewController(animated:true)肯定有问题
我们将不胜感激,谢谢。
答案 0 :(得分:0)
再次打开应用程序时,您的根视图控制器是tabbBar。 您正在使用登录视图更改root。对我来说,在根视图中更改根视图并弹出到新的根目录很奇怪。请制作一个新的故事板进行登录和注册。并删除userDefaults后调用此代码
let storyboard = UIStoryboard(name: "Authentication", bundle: Bundle.main)
let viewController = storyboard.instantiateInitialViewController()
if let viewController = viewController {
self.present(viewController, animated: true, completion: nil)
}
您可以在appDelegate中检查userDefaults并在正确的故事板上享用午餐。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// check your userdefaults and change this codes for loged in user
let storyboard = UIStoryboard(name: "Authentication", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
//
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
不要忘记为视图控制器设置情节提要ID