objective-c - 使用选项卡栏 Controller ( Storyboard)阻止访问 UIViewControllers

标签 objective-c ios authentication uiviewcontroller uistoryboard

我的 iOS 应用程序目前遇到问题。
我正在尝试采用渐进式登录模式,即:用户无需登录即可访问某些应用程序。

所需的功能如下:

  • 用户始终可以查看所有需要登录的导航项
  • 当用户尝试访问需要登录的 uiview( Controller )时,系统会提示他们使用 UIAlertView 要求他们登录。(最好是当应用程序识别出启动的 segue 目标受限时,UIAlertView 会出现)。

起初我使用了 UIViewController 的一个子类,在指定的初始化程序 (initWithCoder) 中,它会检查 NSUserDefaults 以查看用户是否已登录。然后我将其子类化。限制如下:

  • 无法使用 UIViewController 的其他子类,即 UITableViewController
  • UIAlertView 在 View 出现后出现,我假设如果子类 UIViewController 假定用户已登录,这将导致错误。

问题总结:
我想知道如何有条件地阻止用户访问某些 UIView(Controller) 和 UIViewController 的子类,并在发生这种情况时显示 UIAlertView。

更新 1
类别和/或协议(protocol)能否成为可行的解决方案?

更新 2
CodaFi 指出单例是管理用户状态的绝佳解决方案。

实现后,我现在需要弄清楚如何控制用户的访问。
当我使用 Storyboard时,我觉得最通用的实现是子类化 UIStoryboardSegue,并在执行方法上检查用户是否试图访问受限的 UIViewController(可能受限的 Controller 具有指定所需状态的协议(protocol)属性:登录/出去)。然而这里的陷阱是你不能在 Storyboard图形编辑器中选择 UIStoryboardSegue 的类/子类。我知道我可以通过编程方式完成它,但是这看起来很乏味,因为我必须将 IBActions 之类的添加到执行 segues 的方法中,而且我认为这不会与 navigationController 和 tabbarControllers 等元素的行为方式一起工作。

是否有人有可行的解决方案来限制用户的导航?

更新 3
我已经为这个问题添加了一个答案,但我仍然认为它没有答案,因为我写的答案没有考虑到导航栏 Controller 之间的转折。但是,它可能对某些人有帮助。

最佳答案

所以,我已经回答了如何使用自定义 segues 来做到这一点。

现在我明白我真正的问题是与 tabbarcontrollerdelegate 协议(protocol)的精神脱节。

作为防止访问选项卡的解决方案,我创建了一个类来处理所述选项卡栏 Controller

#import <Foundation/Foundation.h>

@interface TabBarDelegate : NSObject<UITabBarControllerDelegate,UIAlertViewDelegate>{
    UITabBarController *cachedTabBarController;
}

@end

#import "TabBarDelegate.h"

@implementation TabBarDelegate

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSLog(@"%s",__FUNCTION__);
    NSLog(@"Pretending the user isnt logged in");

    if(true){
        NSString *message = [NSString stringWithFormat:@"You require an account to access %@",viewController.tabBarItem.title];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Account Required" message:message delegate:self cancelButtonTitle:@"Okay" otherButtonTitles: @"Login",@"Create Account",nil];
        [alert show];
        //Hold tabbarcontroller property for alert callback
        cachedTabBarController = tabBarController;
        return false;
    }

    return true;
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if(cachedTabBarController){
        switch (buttonIndex) {
            case 1:
                //Login
                [cachedTabBarController performSegueWithIdentifier:@"tabBarToLogin" sender:cachedTabBarController];
                break;
            case 2:
                //Sign up
                [cachedTabBarController performSegueWithIdentifier:@"tabBarToSignup" sender:cachedTabBarController];
                break;
            default:
                break;
        }
        //Release tab bar controller from memory
        cachedTabBarController = nil;
    }
}

@end

然后我在 applicationDidFinishLaunching 中将它连接到我的 appDelegate...

//Hook up tab bar delegate
    mainTabController = (UITabBarController*)self.window.rootViewController;
    mainTabBarDelegate = [[TabBarDelegate alloc] init];
    mainTabController.delegate = mainTabBarDelegate;

瞧瞧

关于objective-c - 使用选项卡栏 Controller ( Storyboard)阻止访问 UIViewControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10184080/

相关文章:

objective-c - 方向更改后居中 UIPageControl

iphone - touchesbegan 与多个 UIImageView 检测到不正确的 UIImageView

ios - SiriKit 错误 : Donating intent is not supported by this application

ios - 尝试在时间间隔后清除粘贴板

javascript - 将 token 传递到我的 Angular 4 应用程序的优雅方式是什么?

spring-mvc - 将 LDAP 和 DB 身份验证与 Spring Security 一起使用

ruby-on-rails - Ruby on Rails 4 身份验证,设计与 bcrypt

设置基本 SDK 后,iPhone 模拟器在 NSKeyedUnarchiver 中崩溃

iphone - ios coredata 搜索大型数据集

iphone - 指向 iphone 应用程序 SSL 连接的好教程的指针?