Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

IOS Reset Password using Parse

I am trying to create am Alert View that let you introduce your email con compare with Parse. Finally when result is succeded parse send you an email giving you the positibility to rest your password.

Here is my code

`- (IBAction)contraseña:(id)sender {

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Direccion de Correo" message:@"Introduzca su correo electronico:" delegate:self cancelButtonTitle:@"Cancelar" otherButtonTitles:@"Aceptar", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];

}

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex != [alertView cancelButtonIndex]) { UITextField *emailTextField = [alertView textFieldAtIndex:0];

    PFQuery *query = [PFUser query];
    [query whereKey:@"email" equalTo:self.emailTextField.text];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    
        if (!error) {
            // The find succeeded.
            if (objects.count > 0) {
                //the query found a user that matched the email provided in the text field, send the email
                [self sendEmail:emailTextField.text];
    
            } else {
    
                //the query was successful, but found 0 results
                //email does not exist in the database, dont send the email
                //show your alert view here
            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
    

    }

    • (void)sendEmail:(NSString *)email{ [PFUser requestPasswordResetForEmailInBackground:email];

    }

My problem exactly is that sendEmail needs to declared identifier. What is that? What I need to do?

Thanks indeed Mayte

Stone Preston
Stone Preston
42,016 Points

can you post the error/warning you are getting. everything looks good to me a a quick glance

5 Answers

Hello Stone. Thanks you so much for your answer. Now I write you the error from Console:

''' 2014-09-07 16:03:36.871 pedidos[824:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for type: (null)' '''

Do you have any idea about it?

Thanks in advance

Stone Preston
Stone Preston
42,016 Points

the problem is you are referencing self.emailTextField.text. you most likely dont have a property called emailTextField, you just created a local text field variable. change it from self.emailTextField to just emailTextField

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex]) {
        //here is where you declare the local text field
        UITextField *emailTextField = [alertView textFieldAtIndex:0];

        PFQuery *query = [PFUser query];
        //reference the local text field here
        [query whereKey:@"email" equalTo:emailTextField.text];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

            if (!error) {
                // The find succeeded.
                if (objects.count > 0) {
                    //the query found a user that matched the email provided in the text field, send the email
                    [self sendEmail:emailTextField.text];

                } else {

                    //the query was successful, but found 0 results
                    //email does not exist in the database, dont send the email
                    //show your alert view here
                }
            } else {
                // Log details of the failure
                NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
        }];
    }

Hello Stone. Now I have most that I need but I find one problem. I am not sure where I need to put the AlertView. I want to have two. One that say: "Ok. We send you an email to reset the password" and the other: " email not found". I do the first but the second I can not so it.

''

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex ==1) { UITextField *emailTextField = [alertView textFieldAtIndex:0];

    PFQuery *query = [PFUser query];
    [query whereKey:@"email" equalTo:self.emailTextField.text];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    
        if (!error) {
            // The find succeeded.
            if (objects.count == 0) {
                //the query found a user that matched the email provided in the text field, send the email
                [self sendEmail:emailTextField.text];
                UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"Correo enviado" message:@"Por favor, revise su correo para resetear su contraseña" delegate:self cancelButtonTitle:@"Cancelar" otherButtonTitles:nil];
                [alertView show];
    
            } else {
    
                UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"Correo no encontrado" message:@"Por favor, introduzca un correo electronico valido" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Aceptar", nil];
                [alertView show];
            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    
    }];
    

    } }

''

Thanks once again Stone. The last question. I have create this property that you have told. I found a problem. This property is empty because I need to join it to email text label that I have already create in SiginView Controller. I am working in LoginViewController. How can I join this information.

Thanks indeed.

Mayte