使用 Twilio 的 phone 号码验证解析用户未保存

Parse user not saving using phone number verification with Twilio

我实现了以下代码以在用户输入他的 phone 号码时创建一个新的 PFUser。

- (IBAction)confirmButtonTapped:(id)sender
{
    // Check to see if a valid phone number is entered
    if ([self.phoneNumberTextField.text hasPrefix:@"+"]){
        self.phoneNumber = self.phoneNumberTextField.text;

        //Create new Parse user
        [self createNewUser];
    }
}

- (void)createNewUser
{
    //Create new Parse user
    PFUser *newUser = [PFUser new];
    newUser.username = self.phoneNumber;
    NSString *alphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    NSMutableString *s = [NSMutableString stringWithCapacity:20];
    for (NSUInteger i = 0U; i < 20; i++){
        u_int32_t r = arc4random() % [alphabet length];
        unichar c = [alphabet characterAtIndex:r];
        [s appendFormat:@"%C", c];
    }
    newUser.password = [NSString stringWithString:s];
    newUser.email = [NSString string];
    [newUser setObject:[NSArray array] forKey:@"contactsSnatched"];
    [newUser setObject:[NSArray array] forKey:@"friends"];
    [newUser setObject:self.phoneNumber forKey:@"phoneNumber"];
    [newUser setObject:@"" forKey:@"firstName"];
    [newUser setObject:@"" forKey:@"lastName"];

    //Set profile picture
    NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"PictureSelect.png"], 0.4);
    NSString *fileName = [NSString stringWithFormat:@"profilePicture.jpg"];
    PFFile *imageFile = [PFFile fileWithName:fileName data:imageData];
    [newUser setObject:imageFile forKey:@"profilePicture"];

    [newUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
        if (!error){
            if (succeeded){
                // Call cloud function to send SMS to the entered phone number
                NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:self.phoneNumber, @"phoneNumber", nil];
                [PFCloud callFunctionInBackground:@"sendVerificationCode" withParameters:dict block:^(id object, NSError *error){
                    if (!error){
                        [self performSegueWithIdentifier:@"confirmNumberSegue" sender:self];
                    } else {
                        NSLog(@"Error calling cloud function: %@", error.localizedDescription);
                    }
                }];
            }
        } else {
            NSLog(@"Error signing up new user: %@", error.localizedDescription);
        }
    }];

}

我在这里想要实现的是在通过云代码发送短信确认之前创建一个新的 PFUser。代码执行但 saveInBackgroundWithBlock: 方法之后的块从未被调用,因此用户未被保存。

这段代码实际上完成了工作:

- (IBAction)confirmButtonTapped:(id)sender
{
    // Check to see if a valid phone number is entered
    if ([self.phoneNumberTextField.text hasPrefix:@"+"]){
        self.phoneNumber = self.phoneNumberTextField.text;

        //Create new Parse user
        [self createNewUser];
    }
}

- (void)createNewUser
{
    //Create new Parse user
    PFUser *newUser = [PFUser new];
    newUser.username = self.phoneNumber;
    NSString *alphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    NSMutableString *s = [NSMutableString stringWithCapacity:20];
    for (NSUInteger i = 0U; i < 20; i++){
        u_int32_t r = arc4random() % [alphabet length];
        unichar c = [alphabet characterAtIndex:r];
        [s appendFormat:@"%C", c];
    }
    newUser.password = [NSString stringWithString:s];
    newUser.email = [NSString string];
    [newUser setObject:[NSArray array] forKey:@"contactsSnatched"];
    [newUser setObject:[NSArray array] forKey:@"friends"];
    [newUser setObject:self.phoneNumber forKey:@"phoneNumber"];
    [newUser setObject:@"" forKey:@"firstName"];
    [newUser setObject:@"" forKey:@"lastName"];

    //Set profile picture
    NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"PictureSelect.png"], 0.4);
    NSString *fileName = [NSString stringWithFormat:@"profilePicture.jpg"];
    PFFile *imageFile = [PFFile fileWithName:fileName data:imageData];
    [newUser setObject:imageFile forKey:@"profilePicture"];

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
        if (!error){
            if (succeeded){
                // Call cloud function to send SMS to the entered phone number
                NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:self.phoneNumber, @"phoneNumber", nil];
                [PFCloud callFunctionInBackground:@"sendVerificationCode" withParameters:dict block:^(id object, NSError *error){
                    if (!error){
                        [self performSegueWithIdentifier:@"confirmNumberSegue" sender:self];
                    } else {
                        NSLog(@"Error calling cloud function: %@", error.localizedDescription);
                    }
                }];
            }
        } else {
            NSLog(@"Error signing up new user: %@", error.localizedDescription);
        }
    }];

}