Storyboard登录屏幕的最佳实践,在注销时处理数据清除 [英] Best practices for Storyboard login screen, handling clearing of data upon logout

查看:165
本文介绍了Storyboard登录屏幕的最佳实践,在注销时处理数据清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Storyboard构建iOS应用。根视图控制器是一个标签栏控制器。我正在创建登录/注销过程,它的工作正常,但我遇到了一些问题。我需要知道设置这一切的最好方法。

I'm building an iOS app using a Storyboard. The root view controller is a Tab Bar Controller. I'm creating the login/logout process, and it's mostly working fine, but I've got a few issues. I need to know the BEST way to set all this up.

我想完成以下任务:


  1. 首次启动应用时显示登录屏幕。当他们登录时,转到选项卡栏控制器的第一个选项卡。

  2. 之后他们启动应用程序时,检查他们是否已登录,并直接跳到第一个选项卡根标签栏控制器。

  3. 当他们手动点击注销按钮时,显示登录屏幕,并清除视图控制器中的所有数据。

到目前为止我所做的是将根视图控制器设置为Tab Bar Controller,并为我的Login视图控制器创建了一个自定义segue。在我的Tab Bar Controller类中,我检查它们是否在 viewDidAppear 方法中登录,然后执行segue: [self performSegueWithIdentifier:@ pushLoginsender:self];

What I've done so far is set the root view controller to the Tab Bar Controller, and created a custom segue to my Login view controller. Inside my Tab Bar Controller class, I check whether they are logged in inside the viewDidAppear method, and a perform the segue: [self performSegueWithIdentifier:@"pushLogin" sender:self];

我还设置了需要执行注销操作的通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutAccount)name:@logoutAccountobject:nil];

I also setup a notification for when the logout action needs to be performed: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutAccount) name:@"logoutAccount" object:nil];

注销时,我清除Keychain的凭据,运行 [self setSelectedIndex:0] 并执行segue再次显示登录视图控制器。

Upon logout, I clear the credentials from the Keychain, run [self setSelectedIndex:0] and perform the segue to show the login view controller again.

一切正常,但我想知道:这个逻辑应该在AppDelegate吗?我还有两个问题:

This all works fine, but I'm wondering: should this logic be in the AppDelegate? I also have two issues:


  • 第一次启动应用时,标签栏控制器会在执行segue之前短暂显示。我已经尝试将代码移动到 viewWillAppear 但是segue将无法提前工作。

  • 当他们退出时,所有数据都是仍在所有视图控制器内。如果他们登录到新帐户,旧的帐户数据仍会显示,直到他们刷新为止。 我需要一种方法在退出时轻松清除这一点。

  • The first time they launch the app, the Tab Bar Controller shows briefly before the segue is performed. I've tried moving the code to viewWillAppear but the segue will not work that early.
  • When they logout, all the data is still inside all the view controllers. If they login to a new account, the old account data is still displayed until they refresh. I need a way to clear this easily on logout.

我愿意重新加工。我已经考虑将登录屏幕设置为根视图控制器,或者在AppDelegate中创建导航控制器来处理所有内容......我只是不确定此时最好的方法是什么。

I'm open to reworking this. I've considered making the login screen the root view controller, or creating a navigation controller in the AppDelegate to handle everything... I'm just not sure what the best method is at this point.

推荐答案

以下是我最终要做的事情。除此之外,您唯一需要考虑的是(a)登录过程和(b)您存储应用数据的位置(在这种情况下,我使用的是单例)。

Here is what I ended up doing to accomplish everything. The only thing you need to consider in addition to this is (a) the login process and (b) where you are storing your app data (in this case, I used a singleton).

如您所见,根视图控制器是我的主选项卡控制器。我这样做是因为在用户登录后,我希望应用程序直接启动到第一个选项卡。 (这可以避免登录视图暂时显示的任何闪烁。)

As you can see, the root view controller is my Main Tab Controller. I did this because after the user has logged in, I want the app to launch directly to the first tab. (This avoids any "flicker" where the login view shows temporarily.)

AppDelegate.m

在这个文件中,我检查用户是否已经登录。如果没有,我按下登录视图控制器。我还处理注销过程,我清除数据并显示登录视图。

In this file, I check whether the user is already logged in. If not, I push the login view controller. I also handle the logout process, where I clear data and show the login view.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // Show login view if not logged in already
    if(![AppData isLoggedIn]) {
        [self showLoginScreen:NO];
    }

    return YES;
}

-(void) showLoginScreen:(BOOL)animated
{

    // Get login screen from storyboard and present it
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    LoginViewController *viewController = (LoginViewController *)[storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
    [self.window makeKeyAndVisible];
    [self.window.rootViewController presentViewController:viewController
                                             animated:animated
                                           completion:nil];
}

-(void) logout
{
    // Remove data from singleton (where all my app data is stored)
    [AppData clearData];

   // Reset view controller (this will quickly clear all the views)
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
   MainTabControllerViewController *viewController = (MainTabControllerViewController *)[storyboard instantiateViewControllerWithIdentifier:@"mainView"];
   [self.window setRootViewController:viewController];

   // Show login screen
   [self showLoginScreen:NO];

}

LoginViewController.m

在这里,如果登录成功,我只是关闭视图并发送通知。

Here, if the login is successful, I simply dismiss the view and send a notification.

-(void) loginWasSuccessful
{

     // Send notification
     [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccessful" object:self];

     // Dismiss login screen
     [self dismissViewControllerAnimated:YES completion:nil];

}

这篇关于Storyboard登录屏幕的最佳实践,在注销时处理数据清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆