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

Ribbit App forgot password

Any ideas on how to set an alert view on forgot email function that only checks with existing emails with in the app?

This is my code:

             [self getEmail];

}

- (void)getEmail {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Email Address" message:@"Enter the email for your account:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex]) {
        UITextField *emailTextField = [alertView textFieldAtIndex:0];
        [self sendEmail:emailTextField.text];
    }
}

- (void)sendEmail:(NSString *)email{
    [PFUser requestPasswordResetForEmailInBackground:email];
} 
            ```

6 Answers

Stone Preston
Stone Preston
42,016 Points

do you mean like check if the email actually exists in the database before running the password reset request? you could query the user class and see if there is a user with that email. if there is the query will return an object (the user with the matching email), if not the objects array will be empty

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:self.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]);
  }
}];

Thank you. The code looks amazing. But I have trouble implementing in my existing code :(

Stone Preston
Stone Preston
42,016 Points

what sort of trouble are you having

I guess I did something wrong because I get a few errors. I says for example"Use of undeclared identifier 'sendEmail'"

I did like this:

- (IBAction)forget:(id)sender {

   [self getEmail];

}

- (void)getEmail {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Email Address" message:@"Enter the email for your account:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", 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];

    }
Stone Preston
Stone Preston
42,016 Points

looks like you didnt close that alert view method. it should look like this

- (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]);
}
}];

}

Thanks!! Great! Works now. But it crashes:( I think the @property (weak, nonatomic) IBOutlet UITextField *emailTextField; that i now added to make the code work needs to be connected to the storyboard. But how do I do that when its in in an alertview?

Stone Preston
Stone Preston
42,016 Points

what errors do you get in the console?

I get it when i ad an email in the alert view. I think self.emailTextField.text]; is the issue

Ribbit[45234:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for type: (null)' *** First throw call stack: ( 0 CoreFoundation 0x00000001030f4495 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000102e5399e objc_exception_throw + 43 2 CoreFoundation 0x00000001030f42ad +[NSException raise:format:] + 205 3 Ribbit 0x0000000100083a99 +[PFInternalUtils assertValidClassForQuery:] + 270 4 Ribbit 0x0000000100073316 -[PFQuery whereKey:equalTo:] + 61 5 Ribbit 0x000000010000e13d -[LogInViewController alertView:clickedButtonAtIndex:] + 301 6 UIKit 0x0000000101f27ec8 -[_UIModalItemsCoordinator _notifyDelegateModalItem:tappedButtonAtIndex:] + 151 7 UIKit 0x0000000101aee53e -[_UIModalItemAlertContentView tableView:didSelectRowAtIndexPath:] + 364 8 UIKit 0x0000000101ac85c2 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1312 9 UIKit 0x0000000101ac86eb -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 221 10 UIKit 0x0000000101a19100 _applyBlockToCFArrayCopiedToStack + 316 11 UIKit 0x0000000101a18f71 _afterCACommitHandler + 460 12 CoreFoundation 0x00000001030bfdc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 13 CoreFoundation 0x00000001030bfd37 __CFRunLoopDoObservers + 391 14 CoreFoundation 0x000000010309f522 __CFRunLoopRun + 946 15 CoreFoundation 0x000000010309ed83 CFRunLoopRunSpecific + 467 16 GraphicsServices 0x00000001042b8f04 GSEventRunModal + 161 17 UIKit 0x0000000101a00e33 UIApplicationMain + 1010 18 Ribbit 0x0000000100021ef3 main + 115 19 libdyld.dylib 0x00000001036795fd start + 1 20 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)