如何在基于选项卡的应用程序中检查用户信任?

时间:2012-03-31 13:34:31

标签: iphone ios uiviewcontroller uikit uitabbarcontroller

我正在构建基于选项卡的iphone应用程序,每个选项卡中的所有信息都与用户相关,这意味着用户必须先登录才能转到每个选项卡。我将用户名/密码输入放在第一个选项卡中,并且在成功登录后我将用户confidencial存储在密钥链中。但是,在用户进入其他选项卡之前检查它的最佳方法是什么?并防止未经授权的用户进入除登录选项卡以外的其他选项卡?我不想在每个视图控制器中进行此验证。

2 个答案:

答案 0 :(得分:1)

您可以在Singleton类中使用方法(例如在app delegate中)。在每个选项卡中,您可以检查用户是否登录

if(appdelegate.userLogine){ //用户是登录节目视图

} else { //发布通知

}

答案 1 :(得分:1)

可以使用UITabBarControllerDelegate完成。

实施它,例如在UIApplication代表内,并将其分配给您的UITabBarController

AppDelegate标题:

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
    //[...]
}
//[...]
@end

AppDelegate实施:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //[...]

    //instanciate and configure your tabbarcontroller
    //[...]

    //assign this instance as the delegate of our tabbarcontroller
    tabBarController.delegate = self;
}

只要用户选择任何选项卡,就会调用以下方法。返回NO意味着选择不应该实际发生。例如,您可以在该情况下显示警告,要求用户先登录。

- (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;
}