0

I'm trying to check if the current user has verified it's email before they proceed to the next step. What am i doing wrong please help thank you. The phone number saving in background works.. When i call on the "SavePhoneInBackground" the app crashes

(The SVProgressHUd is the activity indicator)

-(void) SavePhoneInBackground  {

    PFUser *user = [PFUser currentUser];
    user[@"phone"] = _phone_register.text;

    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

        if (!error) {
            NSLog(@"PhoneUpdatingSucces!");
            _phone_register.text = nil;
            [self checkUserEmailVerify];

        }else {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"Something went wrong! Try again." delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
            [alert show];
            [SVProgressHUD dismiss];
            NSLog(@"There was an error in the registration!");
        }
    }];
}


-(void) checkUserEmailVerify {

    PFUser *user = [PFUser currentUser];

    if (![[user objectForKey:@"emailVerified"] boolValue]) {
        // Refresh to make sure the user did not recently verify
        [user refresh];

        if (![[user objectForKey:@"emailVerified"] boolValue]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Whoops!" message:@"You need to verify your emailadress!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
            return;
            [SVProgressHUD dismiss];
        }
    }
    // This is a triumph.
    [SVProgressHUD dismiss];
    [self performSegueWithIdentifier:@"login2" sender:self];

}

3 Answers3

3
func giveCaketoUser(user: PFUser) {
                if (user.objectForKey("emailVerified").boolValue == true){
                    println("true")
                } else {
                    println("flase")
                }
            }
Codetard
  • 2,441
  • 28
  • 34
2

This is how I did it in my App and it will only let verified email users to access. You will need to modify it slightly for what you want.

- (void)logInViewController:(MyLogInViewController *)logInController didLogInUser:(PFUser *)user {

if (![[user objectForKey:@"emailVerified"] boolValue]){
    NSLog(@"User not validated email");
    [[[UIAlertView alloc] initWithTitle:@"Access restricted" message:@"To access this area please validate your email address" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show];
}
else if ([[user objectForKey:@"emailVerified"] boolValue]){
    NSLog(@"Email validated");
    [self dismissViewControllerAnimated:YES completion:NULL];
    [[[UIAlertView alloc] initWithTitle:@"Welcome back" message:(@"%@", user.username) delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show];
}

// [self dismissViewControllerAnimated:YES completion:NULL];

NSLog(@"Login sucessfull and username is %@",user.username);

}

Hope this helps if you have not worked it out yet!

user3611162
  • 183
  • 1
  • 9
  • This post is related to an issue I am having now. In my case there is no emailVerified field that I can get with [PFUser currentUser] (even using allKeys) though it is present on the server side. Some tests (connecting to another server app) seem to show that the client side is working but I have no idea what I could have missed on the server. – Michel Mar 25 '18 at 08:46
1

Here is something I used in one of my recent projects:

- (BOOL)validateEmail:(NSString *)emailStr {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:emailStr];
}

There are other manipulations of the regex you can use. When I used this method the problem I had was that the project would continue to register the user while still showing my custom alert and NSLog error, but that may have been failure on my end and not the method.

Hope this will send you down the right direction!