2.1.1.2.4 OTP Method
Overview
The OTP Verification process allows users to verify their phone numbers by receiving a one-time password (OTP) via SMS, and then entering the OTP in your app to complete the verification.
Get started
- Initialize VerifySpeed: Follow the initialize instructions.
Verifying Phone Numbers with OTP
Use verifyPhoneNumberWithOtp
to send the OTP to the user's phone number with these parameters:
phoneNumber
: The user's phone number (including country code).verificationKey
: Unique key provided by your backend to ensure verification integrity.
VerifySpeed.shared.verifyPhoneNumberWithOtp(
phoneNumber: "PHONE_NUMBER", // e.g., '+1234567890',
verificationKey: "YOUR_VERIFICATON_KEY",
completion: { _ in }
)
This pod library does not include phone number validation. Since validation occurs server-side, it's best to handle initial validation on your end to reduce user wait times. Use a library like libphonenumber
for efficient phone number validation.
Validating OTP
Create a listener to handle the success and failure cases:
class VerifySpeedListenerHandler: VerifySpeedListener {
var onSuccess: ((String) -> Void)?
var onFailure: ((VerifySpeedError) -> Void)?
func onFail(error: VerifySpeed_IOS.VerifySpeedError) {
onFailure?(error)
}
func onSuccess(token: String) {
onSuccess?(token)
}
}
After the user receives the OTP, call validateOTP
with these parameters:
otpCode
: The OTP received from the user.verificationKey
: Unique key provided by your backend to ensure verification integrity.callBackListener
: Callback interface to handle success and failure cases:onSuccess
: Triggered on successful verification, returning atoken
to retrieve the user's phone number.onFailure
: Triggered if verification fails, providingerror
details including type and message.
let listener = VerifySpeedListenerHandler()
listener.onSuccess = { token in
print("Token: \(token)")
}
listener.onFailure = { error in
print("Error: \(error)")
}
VerifySpeed.shared.validateOTP(
otpCode: otpCode,
verificationKey: verificationKey,
callBackListener: listener
)
Example
For complete implementation examples, see:
Swift UI:
UIKit:
When implementing OTP Verification, look for code comments with the keyword TIP
in the example code for guidance on implementation details.
When testing, you'll need to integrate with your backend using these recommended request/response structures (which you can modify as needed).