Objective-C Requires Chilkat v11.0.0+
Objective-C
Send an SMS verification code with Twilio Verify
See more Twilio Examples
Live SMS. When a user requests a code, pass their phone number in E.164 format (for example, +12025550123) and select the SMS channel. Create a Verify Service in the Twilio Console first. Supply its VA service SID and an API key with access to that service. For a live test, use a phone you control. Messaging and verification charges may apply.
Twilio generates the code and sends it to the phone. A pending response means the application can prompt for the code. Save the returned VE verification SID with the user and phone number that initiated the request, then use it to check the code entered by the user.
Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoStringBuilder.h>
#import <CkoHttpRequest.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
#import <NSString.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: @"/Verifications"];
CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
req.HttpVerb = @"POST";
req.ContentType = @"application/x-www-form-urlencoded";
// Supply the number the user asked to verify. AddParam encodes the leading plus sign.
[req AddParam: @"To" value: @"PHONE_NUMBER_IN_E164_FORMAT"];
[req AddParam: @"Channel" value: @"sms"];
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;
}
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 != @"pending") {
NSLog(@"%@%@",@"Verification status: ",status);
return;
}
if (([[result JsonTypeOf: @"sid"] intValue] != 1) || ([result StringOf: @"sid"] == @"")) {
NSLog(@"%@",@"The pending response did not include a verification SID.");
return;
}
// Save this SID with the pending attempt before prompting the user for the code.
NSLog(@"%@%@",@"Pending verification SID: ",[result StringOf: @"sid"]);