Secure TabBar Items Using Protocol Extensions in Swift

While working on a personal project of mine I had the requirement to add security to few TabBar items, which user can only access if they are logged in. 

Since, the login is required to only few TabBar items I had to differentiate between the authentication required items and non-authentication TabBar items. For this reason I implemented a LoginHandler protocol with an extension method.

The LoginHandler will be implemented by all the view controllers which requires authentication before they can be displayed. 

Screen Shot 2016-12-11 at 3.36.12 PM.png

I added a custom UITabBarController class which is responsible for selecting the TabBar items.

One interesting thing to note is that the MyTabBarController uses extensions to becomes a delegate of UITabBarControllerDelegate. All that is needed is to set the delegate, which can be accomplished in the AppDelegate file.