Verifying the User's Phone Number

When the phone verification is enabled, and an account is registered with a phone number, Kii Cloud will send an SMS message with a verification code. Your application should then ask the user to type in the verification code to complete verification. Logging in with the specified phone number will be allowed only when your user verifies them by submitting the correct code.

The phone verification is also launched when it is enabled and a user modifies their phone number. Logging in with the new phone number will be allowed after the user sends the code in the verification SMS message. The old phone number will be invalidated when the verification process is finished. You can use the KiiUser.pendingPhoneNumber property to check the old (pending) phone number.

Please Note: You must specify a valid mobile phone number in an international phone number format (starting with + and your country code) in order to properly begin the SMS verification process.

Enabling the verification

You can enable or disable the verification in the developer portal. For more information, see Toggle the verifciations.

By default, the verification feature is turned off.

Registering the verification code

After receiving the SMS verification code from the user, your application should invoke the verifyPhoneNumber method to complete the verification process.

Swift:

  • // Get the currently logged-in user.
    let user = KiiUser.current()!
    
    do{
      // Verify the phone number using a code sent via SMS.
      try user.verifyPhoneNumberSynchronous("12345")
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Get the currently logged-in user.
    let user = KiiUser.current()!
    
    // Verify the phone number using a code sent via SMS.
    user.verifyPhoneNumber("12345", with: { (user :KiiUser?, error : Error?) -> Void in
      if (error != nil) {
        // Handle the error.
        return
      }
    })

Objective-C:

  • // Get the currently logged-in user.
    KiiUser *user = [KiiUser currentUser];
    
    NSError *error;
    
    // Verify the phone number using a code sent via SMS.
    [user verifyPhoneNumber:&error withCode:@"12345"];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Get the currently logged-in user.
    KiiUser *user = [KiiUser currentUser];
    
    // Verify the phone number using a code sent via SMS.
    [user verifyPhoneNumber:@"12345"
                  withBlock:^(KiiUser *user, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

Setting custom SMS templates

You can customize the content of the SMS message on the developer portal. See Configuring the phone (SMS) verification to learn more.

Resending the verification SMS message

The verification SMS will be sent automatically to the user when the user registration and the user attribute modification are made.

You can also request to resend the verification SMS. As shown in the sample code below, execute the resendPhoneNumberVerificationSynchronous method while the user is logged-in to request the resend of the verification SMS (for the details on the currentUser method, see Getting the Current User).

Swift:

  • // Get the currently logged-in user.
    let user = KiiUser.current()!
    
    do{
      // Resend a verification SMS message.
      try user.resendPhoneNumberVerificationSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Get the currently logged-in user.
    let user = KiiUser.current()!
    
    // Resend a verification SMS message.
    user.resendPhoneNumberVerification { (user :KiiUser?, error : Error?) -> Void in
      if (error != nil) {
        // Handle the error.
        return
      }
    }

Objective-C:

  • NSError* error = nil;
    
    // Get the currently logged-in user.
    KiiUser* user = [KiiUser currentUser];
    
    // Resend a verification SMS message.
    [user resendPhoneNumberVerificationSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Get the currently logged-in user.
    KiiUser* user = [KiiUser currentUser];
    
    // Resend a verification SMS message.
    [user resendPhoneNumberVerificationWithBlock:^(KiiUser *user, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];