简体   繁体   中英

iOS Login Screen using Storyboard [Mediator Design Pattern?]

I have a login screen with with ip, port, username and password text fields to get the values and connect to a ASMX webservice. Connecting works flawless.

What's the best way to realize a login cycle? Define a blank view controller as the rootViewController and implement a protocol in the LoginViewController to delegate to this blank viewController and check the login credentials there and if succeeded to link to the main view? I'm into clean and decoupled code. So I decided to use the Mediator Design Pattern.

I have a JSONHelper class who will do the serialization. But I don't know whether to delegate to the loginVC or use an instance of it in the blank view controller (if it's the right way).

You may want to start with the view you want to present at first and check if authentication is needed. If it is, present a view controller modally without animation. Let's say you have the following view controllers:

  • InitialViewController: The root view controller in your storyboard
  • LoginViewController: The screen where a user can enter her credentials
  • AuthenticationHelper: Another class that deals with authentication. It could store username and pass in the keychain, retrieve them on startup and automatically try to log the user in.

As soon as the app launches, you can ask the AuthenticationHelper to log the user in. If it already has the username and password stored, it can do it automatically without presenting the login screen. If it doesn't, it can then show LoginViewController.

Note the following:

  • This is useful also if by any cause the user is logged out in the middle of a session or if you change the password server side and need to ask the user to authenticate again. AuthenticationHelper should be in charge of presenting LoginViewController whenever the user has enter credentials.
  • As soon as authentication succeeds (or fails), AuthenticationHelper should fire a NSNotification to notify other ViewControllers of the event. Also, when the authentication succeeds, AuthenticationHandler should dismiss the LoginViewController

Once the login is successful you could just preform a manual segue from it to the main view controller. Thats only a very small amount of code. No delegates and such. Just something like this:

    if (login){
[self performSegueWithIdentifier:@"YOUR SEGUE ID"]; 
}

Than method then calls the prepareForSegue method, where you could pass values to the main view controller if you like. Xcode will take care of all the memory management for you and the recycling of views.

Of course, you need to be using storyboards for this. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM