gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:14:47 24 4
gpt4 key购买 nike

我正在构建一个基于选项卡的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com