iphone - 如何在基于选项卡的应用程序中检查用户 secret ?

标签 iphone ios uiviewcontroller uikit uitabbarcontroller

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

最佳答案

这可以使用 UITabBarControllerDelegate 来完成。

实现它,例如在您的 UIApplication 委托(delegate)中并将其分配给您的 UITabBarController

AppDelegate header :

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

关于iphone - 如何在基于选项卡的应用程序中检查用户 secret ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956321/

相关文章:

iphone - 游戏设计 : Checking for object intersection or getting values from accelerometer

iphone - objective-c - 有没有办法用动画更改 leftBarButtonItem?

iphone - Objective-C UIRefreshControl 不工作

ios - React-native 错误需要未知模块 'react'

swift - 管理同一 View Controller 的多个实例

ios - 在 swift 中关闭模态 viewController 时传递数据

ios - 将 UICollectionView header 中的 UISearchBar 隐藏在 NavigationBar 后面

iphone - Objective-C:AutoresizingMask + 2 Subviews = 挣扎

objective-c - 自动旋转 iOS6

ios - 以编程方式在 Xcode Playgrounds 中创建 UIView 子类