4

Is it possible that when a user clicks on a Tab bar item that I can override this in the UITabBarController , where I then check a UserDefault which then decides whether I show the view or return and they stay on their current view ?

It would be something like this:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if item.image == UIImage(named: "TabProfile")
    {
        // Profile tab selected
        if !loginController.isUserLogged()
        {
            // Not logged in...
            showLoginView()  

            // Following line doesn't work...
            tabBarController?.selectedIndex = selectedIndex
        }
    }
}

If possible I want to perform this check and if false then actually prevent the view from even reaching viewDidLoad.

Thanks.

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Niall Kiddle
  • 1,477
  • 1
  • 16
  • 35
  • 1
    Wouldn't it be better to avoid adding the tab at all until the user is logged in? Or at least disable the tab bar item until the user logs in? – rmaddy Jan 10 '19 at 23:13
  • @rmaddy I thought about this, but I want the user to be able to navigate all content without having to create an account. – Niall Kiddle Jan 10 '19 at 23:36
  • Your whole question is asking how to conditionally prevent a user from selecting a tab. Maybe I put the wrong condition in my comment but it still stands. Don't add (or just disable) the tab until the condition is met. – rmaddy Jan 10 '19 at 23:38
  • In this case the interaction between the guest user and the profile tab bar item displays the login popup. Which is the desired functionality. Maybe I worded the question wrong and didn't given enough context. Thank you, anyway – Niall Kiddle Jan 10 '19 at 23:42

1 Answers1

2

I guess you need

func tabBarController(_ tabBarController: UITabBarController, 
              shouldSelect viewController: UIViewController) -> Bool {
   if let ind = tabBarController.viewControllers!.index(of:viewController) , ind == 2 { // suppose profile is 2
      // 
       if userNotLogged { 
         // present modal login view 
         return false
       } 
   } 
  return true
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87