0

I don't want the function to return false if the user enters invalid login credentials. I made a __block BOOL but that hasn't worked. I took a look at this post Return value for function inside a block and I don't understand how to implement a callback. How would that work in this scenario?

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {

__block BOOL ret=YES;

NSString *phoneNumberField = [self.phoneNumberField.text stringByTrimmingCharactersInSet:
                              [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *passwordField = [self.passwordField.text stringByTrimmingCharactersInSet:
                           [NSCharacterSet whitespaceAndNewlineCharacterSet]];

PFUser *newUser = [PFUser user];
newUser.username = phoneNumberField;
newUser.password = passwordField;

if ([identifier isEqualToString:@"Verify"]) {

    if([newUser.username length] == 0 || [newUser.password length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops! Sorry"
                                                            message:@"Please make sure you enter both a phone number and a password!" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        return NO;
    }
    else
    {
        //PROBLEM IS BELOW
        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
            if(!error)
            {
                ret = YES;
                NSLog(@"Successful login");
            }
            else
            {
                ret = NO;
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"That phone number is already in use" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alertView show];
            }
        }];
    }
    return ret;
}  else {
    return YES;
}
}

Return value for function inside a block

Community
  • 1
  • 1
Michael McKenna
  • 811
  • 1
  • 11
  • 24
  • Can't return like that. You must create a function and call it in `signUpInBackgroundWithBlock` `if(!error){ // call function}else{// call function}` – tuledev Nov 13 '15 at 06:59
  • What _specifically_ do you not understand about the question you found? There's one basic way to deal with this situation, and that's it. You need a callback of some kind. – jscs Nov 13 '15 at 07:02
  • See also: [How do I return a value determined within a delay after block?](http://stackoverflow.com/q/22877902) [How can I retrieve a return value from a completion block?](http://stackoverflow.com/q/8432134) [Function return value from block](http://stackoverflow.com/q/8011031) – jscs Nov 13 '15 at 07:03
  • @JoshCaswell Hey I edited my question. My lack of understanding of callbacks is the problem. Ill take a look at your links. – Michael McKenna Nov 13 '15 at 07:04

0 Answers0