Objective-C Requires Chilkat v11.0.0+
Objective-C
Check an SMS verification code with Twilio Verify
See more Twilio Examples
After the user enters the SMS code, retrieve the VE verification SID saved when you started the verification. Send that SID and the entered code to the same Verify Service. Keep the code as text so a leading zero survives.
Mark the phone number on that pending attempt as verified only when Twilio returns status=approved. A pending check leaves the number unverified. Twilio can return HTTP 404 after expiration, approval, or exhaustion of check attempts. Keep a successful result in your application so it can be used after Twilio removes the verification.
Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoStringBuilder.h>
#import <CkoHttpRequest.h>
#import <NSString.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.BasicAuth = YES;
http.Login = @"YOUR_API_KEY_SID";
http.Password = @"YOUR_API_KEY_SECRET";
// Both requests use the same Verify Service SID (VA...).
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://verify.twilio.com/v2/Services/"];
[url Append: @"YOUR_VERIFY_SERVICE_SID"];
[url Append: @"/VerificationCheck"];
CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
req.HttpVerb = @"POST";
req.ContentType = @"application/x-www-form-urlencoded";
// Load the saved SID for this user's pending attempt. Take only the code from user input.
NSString *verificationSid = @"SAVED_VERIFICATION_SID";
NSString *enteredCode = @"CODE_ENTERED_BY_USER";
[req AddParam: @"VerificationSid" value: verificationSid];
[req AddParam: @"Code" value: enteredCode];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpReq: [url GetAsString] request: req response: resp];
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",@"No response from Twilio. Check connectivity and TLS settings.");
return;
}
if ([resp.StatusCode intValue] == 404) {
NSLog(@"%@",@"No active verification found. Check the service and verification SIDs.");
return;
}
CkoJsonObject *result = [[CkoJsonObject alloc] init];
BOOL parsed = [result Load: resp.BodyStr];
if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
NSLog(@"%@%d",@"Twilio returned HTTP ",[resp.StatusCode intValue]);
if ((parsed == YES) && ([[result JsonTypeOf: @"code"] intValue] == 2)) {
NSLog(@"%@%d",@"Twilio error code: ",[[result IntOf: @"code"] intValue]);
}
return;
}
if ((parsed != YES) || ([[result JsonTypeOf: @"status"] intValue] != 1)) {
NSLog(@"%@",@"Verification status missing from the response.");
return;
}
NSString *status = [result StringOf: @"status"];
if (status == @"approved") {
// Record success for the user and phone number associated with verificationSid.
NSLog(@"%@",@"Phone number verified.");
}
else {
if (status == @"pending") {
NSLog(@"%@",@"Code not accepted. Check the digits and enter the code again.");
}
else {
NSLog(@"%@%@",@"Phone number unverified. Verification status: ",status);
}
}