Suggestions to manage Login/Logout transitions

2019-07-10 13:25发布

问题:

I have an application that can be used only if the user is authenticated. In particular, I created two different UIViewController. The first is called LoginViewController while the second is called HomeViewController. In applicationDidFinishLaunching: method, LoginViewController is created and then added to rootViewController property like this:

LoginViewController* loginCtr = ... // alloc and initiWithNibName... 
self.window.rootViewController = loginCTr;
[loginCtr release];

Whitin LoginViewController I created a method that performs the login. When the user has been authenticated, I perform a method, called performLogin.

- (void)performLogin
{
  MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  [appDelegate switchView];    
}

where swicthView method has been implemented inside the Application delegate class.

- (void)switchView
{
   if(VIEW_TYPE == kLogin) // Display Login
   {
     // create a new LoginViewController and assign it to rootViewController
   }

   else // Display Home
   {
     // create a new HomeViewController and assign it to rootViewController
   }
}

Given the previous code, is it possible to implement a more elegant mechanism to manage login / logout transition or does this type of implementation could be considered a valuable solution?

Thank you in advance.

回答1:

Another option, I will set the HomeViewController as the rootViewController. In the viewDidLoad or viewDidAppear method (before I display any information on the HomeViewController), I will check whether the user has login credential. If not, I will display the loginViewController as a modal to request user login credential. In this case, I don't need to change the rootViewController of the window.



回答2:

Starting from iOS 5 I started to manage login/logout transitions using the new containment API for UIViewControllers.

Implementing a Container View Controller

If iOS 5 is not available I would see a similar approach writing-high-quality-view-controller.

Hope that helps.