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
}
3
When you signout from the app, you have to set your signIn view controller as the root view controller like this.
当您从应用程序注销时,您必须将signIn视图控制器设置为这样的根视图控制器。
AuthService.signOut(onSuccess: {
// Present the Sign In VC
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signInVC = storyboard.instantiateViewController(withIdentifier: "signInViewController") as! signInViewController
let nav = UINavigationController(rootViewController:signInVC)
let application = UIApplication.shared.delegate as! AppDelegate
application.window!.rootViewCOntroller= nav
}) { (errorMessage) in
ProgressHUD.showError(errorMessage)
}
Now in AppDelegate.swift file, in application(_:didFinishLaunchingWithOptions:) method, you have to check that user is nil or not.
现在在AppDelegate.swift文件中,在application(_:didFinishLaunchingWithOptions :)方法中,你必须检查用户是否为nil。
if API.User.CURRENT_USER != nil {
let mainNav = self.window?.rootViewController as! UINavigationController
let storyBoard = UIStoryboard(name: "tabBar", bundle:nil)
let sWRevealViewCOntroller= storyBoard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
mainNav.pushViewController(sWRevealViewController, animated: false)
}
NOTE: Please add your storyboard Id and View controller name in identifier name and view controller name.
注意:请在标识符名称和视图控制器名称中添加故事板ID和视图控制器名称。
0
Instead of navigation(performSegue), you have to change your RootViewController and set signInViewController as root VC.
您必须更改RootViewController并将signInViewController设置为root VC,而不是导航(performSegue)。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let obj = storyboard.instantiateViewController(withIdentifier: "signInViewController") as! signInViewController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewCOntroller= obj
Also, in your application(_:didFinishLaunchingWithOptions:) you have to check for your user session
此外,在您的应用程序(_:didFinishLaunchingWithOptions :)中,您必须检查您的用户会话
if API.User.CURRENT_USER != nil {
//SET YOUR ROOT VIEW CONTROLLER HERE
}
You need set your root VC as, signInViewController or revealViewController(when the user exists)
您需要将根VC设置为signInViewController或revealViewController(当用户存在时)