简体   繁体   中英

IOS App switch views after login

I'm learning to develop for IOS in Xcode 5. The app uses a master-detail template which it populates from a simple API i've written in PHP.

I want users to log into the app so that the app can make requests to the API on behalf of the user. I'll explain what i've set up (which could be entirely wrong):

I've created a login view and made it the initial view. My login view is controlled by a class named LoginViewController which contains a 'logUserIn' method. When the 'Login' button in the view is pressed this method sends a post request to the API to check the user's credentials, my app then reads the response to see if they are valid. This is where i'm stuck.

After i've determined I have a valid set of credentials I want to do 2 things:

  1. Save the credentials for use on subsequent requests
  2. Switch from the login view back to the master-detail view

If when the app loads there are already valid credentials saved the loginViewController should switch straight to the master-detail view.

Here's my LoginViewController.m:

#import "NTFYLoginViewController.h"

@interface NTFYLoginViewController ()

@end

@implementation NTFYLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Attempt to read saved credentials
    // Check they still work
    // Switch to master-detail view if credentials exist and are valid
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)logUserIn:(id)sender
{
    NSString *username = self.usernameField.text;
    NSString *password = self.passwordField.text;

    // The code here talks to the api and checks the credentials, 
    // i've swapped it for pseudo code below as the actual code 
    // is irrelevant

    if(user is logged in)
    { 
        // Save credentials
        // Switch to master-detail view
    }

    // Display login error    
}

@end

So, here is what I actually want to know:

  1. Is this the best way to go about doing this?
  2. How can I switch to the MasterViewController from my LoginViewController?

Use the KeyChain services to save the login credentials, as well as other sensitive information that you do not want to be easily accessible by an attacker.

To go back to your master view controller, there are several approaches you can take. The easiest would be to display the login view controller modally on top of the master when the app launches (or when it determines the user needs to enter credentials). Once the user enters the credentials and you determine the user is authenticated, dismiss the login view controller back to the master view controller.

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