5

I am using UITabBarController and added tabs using relationship segue from storyboard.

How to hide particular tab according to logged in user role?

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Could you elaborate the question? you might also need to add a code snippet of what have you tried so far... – Ahmad F Aug 22 '17 at 09:35

2 Answers2

4

Nice question!

You need to dig UITabbarController and its members (properties + functions)

Now, focus on these steps to find solution:

  1. You can create as many tabbar (item) as you want, using View controllers assocated with it by Segue. Connect all view controllers with tabbar (controller) using segue in your story board.
  2. Now, about UITabbarController: It has a property (variable) viewControllers (which an array of UIViewController) that stores UIViewControllers for your Tabbar Controller associated using UITabbar items.
  3. You need to store a value, that indicates user login status, in permanent storage (like UserDefaults, CoreData,....) of device.
  4. When your application launches, retrieve/get that value (user login status) from storage and use it for further operations on UITabbarController.
  5. Now focus on step 2: Get all view controllers programatically from your tabbar controller using property 'viewControllers` and store in separate variable.
  6. Interchange/Exchange/Update position (index) of ViewControllers in array (You can also remove or insert view controller programatically) according to your requirement (login status) and reassign the same array (variable) to viewControllers property of tabbar controller.

Here is sample example on application launch:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if var tabController = self.window?.rootViewController as? UITabbarController, var viewControllers = tabController.viewControllers {

       let isLoggedIn = <get value from your data storage as bool>

       if  isLoggedIn {
           viewControllers.remove(at: firstIndex)  // By considering you need to remove view controller at index first. It will automatically remove tab from tabbar also.
           tabController.viewControllers = viewControllers
           self.window?.rootViewController = tabController
           // further operations to make your root controller visible....
       }

   }

}
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • How to initialise window? Sorry I'm new to ios, [https://stackoverflow.com/questions/28384321/remove-tabbar-item-in-swift] also referred this link but its not worked – Shubhangi Shedage Aug 22 '17 at 10:22
  • In my code - `self.window?` is property of `AppDelegate`. You not need to initialize it. Just create Your tabbar controller from storyboard. Assign main storyboard and root constroller as tabbar to your project from your storyboard. This code with automatically work. – Krunal Aug 22 '17 at 10:24
  • I have login as root controller. User logged in successfully then TabBarController is open.I have already added all the tabs in TabBarController from storyBoard and its working fine. Now I want to show and hide view controllers according to user role (which I get from server in login response) – Shubhangi Shedage Aug 22 '17 at 10:56
  • Response from server- "featuresAction" : “[{\”Frature1\”:[\”View\”]},{\”Frature2\”:[\”View\"]},{\"Frature3\”:[\”View\"]},{\"Frature4\”:[\”View\"]},{\"Frature5\”:[\”View\"]}]" This is response and I want to hide tabs according to feature list(which I get from server) – Shubhangi Shedage Aug 22 '17 at 11:24
  • TabBarControllers: – Shubhangi Shedage Aug 22 '17 at 11:24
3

If you want to remove tabs from your tab bar controller do something like this (When your user is not logged in)

NSInteger indexToRemove = 0;
NSMutableArray *tabs = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabs removeObjectAtIndex:indexToRemove];
self.tabBarController.viewControllers = tabs;

when your user logs in

UIViewController *viewController = [[UIViewController alloc] init];
NSMutableArray *tabs = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabs addObject:viewController];
self.tabBarController.viewControllers = tabs;

Swift Version

Remove tab

let indexToRemove = 0
if var tabs = self.tabBarController?.viewControllers {
  tabs.remove(at: indexToRemove)
  self.tabBarController?.viewControllers = tabs
} else {
  print("There is something wrong with tabbar controller")
}

Add tab

let indexToAdd = 2
let vc = UIViewController.init()
if var tabs = self.tabBarController?.viewControllers {
  tabs.append(vc) // Append at last index of array
  // tabs.insert(vc, at: indexToAdd)  // Insert at specific index
  self.tabBarController?.viewControllers = tabs
} else {
  print("There is something wrong with tabbar controller")
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
Pratik Jamariya
  • 810
  • 1
  • 10
  • 35