Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Validate and format a phone number with Twilio Lookup

See more Twilio Examples

Use Basic Lookup v2 to check a national-format number and obtain its E.164 and national display formats. Supply the two-letter country code for a national number. The example requests only the basic fields.

Read the boolean valid field even when the HTTP request succeeds. When it is false, display any validation_errors that Twilio supplies. Numbering-plan validation does not establish reachability or control of a phone. To confirm control, send an SMS verification code.

Twilio documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <CkoStringBuilder.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Load an API key for the Twilio account from private application configuration.
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.BasicAuth = YES;
http.Login = @"YOUR_API_KEY_SID";
http.Password = @"YOUR_API_KEY_SECRET";

//  The phone number is one URL path segment. Percent-encode spaces and any leading +.
CkoStringBuilder *number = [[CkoStringBuilder alloc] init];
[number Append: @"020 7946 0018"];
BOOL encoded = [number Encode: @"url_rfc3986" charset: @"utf-8"];
if (encoded != YES) {
    NSLog(@"%@",@"Could not encode the phone number.");
    return;
}

CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://lookups.twilio.com/v2/PhoneNumbers/"];
[url Append: [number GetAsString]];
[url Append: @"?CountryCode=GB"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];

success = [http HttpNoBody: @"GET" url: [url GetAsString] response: resp];
if ([resp.StatusCode intValue] == 0) {
    NSLog(@"%@",@"No HTTP response from Twilio.");
    return;
}

BOOL parsed = [reply Load: resp.BodyStr];
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
    NSLog(@"%@%d",@"Twilio returned HTTP ",[resp.StatusCode intValue]);
    if ((parsed == YES) && ([[reply JsonTypeOf: @"code"] intValue] == 2)) {
        NSLog(@"%@%d%@%@",@"Twilio error: ",[[reply IntOf: @"code"] intValue],@"  ",[reply StringOf: @"message"]);
    }

    return;
}

if (parsed != YES) {
    NSLog(@"%@",@"Expected a JSON response from Twilio.");
    return;
}

if ([[reply JsonTypeOf: @"valid"] intValue] != 5) {
    NSLog(@"%@",@"The lookup response is missing the validity flag.");
    return;
}

if ([reply BoolOf: @"valid"] != YES) {
    NSLog(@"%@",@"The number is invalid for the specified country.");
    int i = 0;
    while ((i < [[reply SizeOfArray: @"validation_errors"] intValue])) {
        reply.I = [NSNumber numberWithInt: i];
        NSLog(@"%@",[reply StringOf: @"validation_errors[i]"]);
        i = i + 1;
    }

    return;
}

if ([[reply JsonTypeOf: @"phone_number"] intValue] != 1) {
    NSLog(@"%@",@"The lookup response is missing the E.164 number.");
    return;
}

NSLog(@"%@%@",@"E.164: ",[reply StringOf: @"phone_number"]);
NSLog(@"%@%@",@"National format: ",[reply StringOf: @"national_format"]);