作者:k78283381 | 来源:互联网 | 2023-12-12 11:13
本文介绍了在iOSSwift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。
I have different story board called Main, tabBar, home, map, etc. In tabBar storybard, I have used SWRevealViewController view and initiated as initially view. In main Storyboard only two are used namely sign in and sign up controller.
我有不同的故事板叫做Main,tabBar,home,map等。在tabBar的故事栏中,我使用了SWRevealViewController视图并作为初始视图启动。在主要的Storyboard中只使用了两个即登录和注册控制器。
Here is my screenshot of tabBar storyBoard 
这是tabBar storyBoard的截图
My question is when user logout and come back it automatically goes to home screen instead of going to sign in screen [This issue is due to SWRevealViewController is initial view controller].
我的问题是当用户注销并返回时它自动进入主屏幕而不是登录屏幕[此问题是由于SWRevealViewController是初始视图控制器]。
Here code i tried : In sign view controller
我试过这里的代码:在符号视图控制器中
In viewDidAppear check user available in firebase or not
在viewDidAppear中检查firebase中是否有可用的用户
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if API.User.CURRENT_USER != nil {
// segue to the Tab Bar Controller
self.performSegue(withIdentifier: "signInToTabBar", sender: nil)
}
}
Sign in action:
登录动作:
@IBAction func SignInButton(_ sender: Any) {
view.endEditing(true)
guard
let email = emailTextField.text, !email.isEmpty,
let password = passwordTextField.text, !password.isEmpty
else {
self.showErrorAlert(message: "Username or email or passowrd should not be empty")
return
}
// show the progress to the user
ProgressHUD.show("Starting sign-in...", interaction: false)
// use the signIn class method of the AuthService class
AuthService.signIn(email: emailTextField.text!, password: passwordTextField.text!, onSuccess: {
// on success segue to the Tab Bar Controller
API.User.observeCurrentUser { user in
guard let currentUser = Auth.auth().currentUser else {
return
}
PrefsManager.sharedinstance.UIDfirebase = currentUser.uid
PrefsManager.sharedinstance.username = user.username!
PrefsManager.sharedinstance.userEmail = user.email!
PrefsManager.sharedinstance.imageURL = user.profileImageURL!
ProgressHUD.showSuccess("Sucessfully signed in.")
self.performSegue(withIdentifier: "signInToTabBar", sender: nil)
}
}, onError: { errorString in
ProgressHUD.dismiss()
self.showErrorAlert(message: errorString ?? "Server error")
})
}
}
}
SWRevealViewController menu table i am listing menu like home, bookings, profile, logout :
SWRevealViewController菜单表我列出了菜单,如家,预订,个人资料,注销:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MenuTableViewCell
cell.menuName.text = menuName[indexPath.row]
cell.menuIcon.image = UIImage(named: menuImage[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// tableView.deselectRow(at: indexPath, animated: true)
let row = indexPath.row
if row == 0{
let storyboard = UIStoryboard(name: "Home", bundle: nil)
let obj = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let navCOntroller= UINavigationController(rootViewController: obj)
navController.setViewControllers([obj], animated:true)
navController.tabBarController?.tabBar.isHidden = false
self.revealViewController().setFront(navController, animated: true)
self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
} else if row == 1{
let storyboard = UIStoryboard(name: "Bookings", bundle: nil)
let obj = storyboard.instantiateViewController(withIdentifier: "BookingsViewController") as! BookingsViewController
let navCOntroller= UINavigationController(rootViewController: obj)
navController.setViewControllers([obj], animated:true)
self.revealViewController().setFront(navController, animated: true)
self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
} else if row == 2 {
let storyboard = UIStoryboard(name: "Profile", bundle: nil)
let obj = storyboard.instantiateViewController(withIdentifier: "profileViewController") as! profileViewController
let navCOntroller= UINavigationController(rootViewController: obj)
navController.setViewControllers([obj], animated:true)
self.revealViewController().setFront(navController, animated: true)
self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
} else if row == 3 {
print(indexPath)
// Log out user from Firebase
AuthService.signOut(onSuccess: {
// Present the Sign In VC
// PrefsManager.sharedinstance.logoutprefences()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signInVC = storyboard.instantiateViewController(withIdentifier: "signInViewController")
self.present(signInVC, animated: true)
// self.navigationController?.pushViewController(signInVC, animated: true)
}) { (errorMessage) in
ProgressHUD.showError(errorMessage)
}
}
}
IN home view controller, checking user available or not:
在家庭视图控制器中,检查用户是否可用:
override func viewDidAppear(_ animated: Bool) {
if API.User.CURRENT_USER != nil {
// segue to the Tab Bar Controller
self.performSegue(withIdentifier: "signInToTabBar", sender: nil)
}
super.viewDidAppear(true)
self.tabBarController?.tabBar.isHidden = false
}

2 个解决方案