Question

I am working on my app's login screen and cannot get it to function properly and have been trying to fix this myself for the past 2 hours. I have set me Login screen's View Controller as the delegate for two protocols. One of these protocols contains an optional BOOL method that stops the login process if the user enters in a username and password that do not meet certain criteria.

The bool method that is declared in the protocol looks like this:

- (BOOL)logInViewController:(PFLogInViewController *)logInController     shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password;

Anyways, when the user enters in and submits their login information, if the lengths of the username and password that they entered in are NOT equal to 0 then the login info is submitted to a web server.

However, if the username and password lengths are equal to 0 then the BOOL returns NO and is supposed to pop up an alert window with the title of "Missing Information" and a message of ""Make sure you fill out all of the information!"

For some reason, when I test this on my iPhone and try logging in with an empty username and password, it still submits to the server. It should not be doing this. It should be triggering the BOOL method and popping up the correct alert window.

I really appreciate the help.

Below in my Login View Controller's main file, you will see that I have implemented the BOOL method above the viewDidAppear method, and then the actual method call is included inside of the viewDidAppear method.

Here is my login screen's main file:

#import "ParseLoginViewController.h"
#import <Parse/Parse.h>

@interface ParseLoginViewController () <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate>

@end

@implementation ParseLoginViewController

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

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.    
}

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

- (BOOL)logInViewController:logInController shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password {
// Check if both fields are completed
if (username && password && username.length != 0 && password.length != 0) {
    return YES; // Begin login process
}

[[[UIAlertView alloc] initWithTitle:@"Missing Information"
                            message:@"Make sure you fill out all of the information!"
                           delegate:nil
                  cancelButtonTitle:@"ok"
                  otherButtonTitles:nil] show];
return NO; // Interrupt login process
}

-(void)viewDidAppear
{
if (![PFUser currentUser]) { // No user logged in
    // Create the log in view controller
    PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
    [logInViewController setDelegate:self]; // Set ourselves as the delegate

    // Create the sign up view controller
    PFSignUpViewController *signUpViewController = [[PFSignUpViewController alloc] init];
    [signUpViewController setDelegate:self]; // Set ourselves as the delegate

    // Assign our sign up controller to be displayed from the login controller
    [logInViewController setSignUpController:signUpViewController];

    // Present the log in view controller
    [self presentViewController:logInViewController animated:YES completion:NULL];

    NSString *username = [[NSString alloc]init];
    NSString *password = [[NSString alloc]init];
    PFLogInViewController* logInController = [[PFLogInViewController alloc]init];

    [self logInViewController:logInController shouldBeginLogInWithUsername:username    password:password];

}

}

@end
Was it helpful?

Solution

I am not sure why you have the BOOL method. The simple way of doing this is to check the length of the username, then check the length of the password before sending the POST request to the server. So wherever you call the method to send the post request just check the username and password.

Example:

-(void)someMethod
{
    //whatever is already here...

    if (username.length && password.length != 0)
        {
            [self callPOSTRequestMethod];
        }
    else
        {
            //create and show your alert...
        }

It should be that simple if it's all contained within a single VC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top