I am building a tab based iphone application, all the information in each tab is user related, which means the user have to login before go to each tab. I put the username/password inputs in the first tab, and I will store the user confidencial in key chain after a successful login. However what's the best way to check it before the user enter the other tabs? and prevent the unauthorized user to enter the other tabs except login tab? I don't want to do this verification in every view controllers.
Asked
Active
Viewed 107 times
1
-
+1 for using the keychain for user credentials. – Till Mar 31 '12 at 18:50
2 Answers
1
You can use method in Singleton class (like in app delegate). In every tab you can check whether user is login like
if(appdelegate.userLogine) { // User is login show view
}else { // Post Notification
}

Mangesh
- 2,257
- 4
- 24
- 51
-
Thanks, but is there a centre place to do this check instead of invoking the checking logic in every tab controllers? – scottliyq Mar 31 '12 at 15:17
1
That could be done using the UITabBarControllerDelegate
.
Implement it e.g. within your UIApplication
delegate and assign it to your UITabBarController
.
AppDelegate Header:
@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
//[...]
}
//[...]
@end
AppDelegate Implementation:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//[...]
//instanciate and configure your tabbarcontroller
//[...]
//assign this instance as the delegate of our tabbarcontroller
tabBarController.delegate = self;
}
The following method gets called whenever the user selects any tab. Returning NO means the selection should not actually happen. You could, for example, in that situation show an alert asking the user to login first.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
//is the user logged in and did the user select any but the first tab?
if (!userLoggedIn &&
[tabBarController.viewControllers indexOfObject:viewController] != 0)
{ //nope->...
//force the user to the first tab
tabBarController.selectedIndex = 0;
//prevent the originally chosen tab selection
return NO;
}
//user is logged in, it is safe to select the chosen tab
return YES;
}

Till
- 27,559
- 13
- 88
- 122
-
Thanks, one more question, once the validation fails, can I force the user to go to the first tab? – scottliyq Apr 01 '12 at 04:49