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

Send an SMS with Twilio

See more Twilio Examples

Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.

For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.

Twilio Messages API reference

Chilkat Objective-C Downloads

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

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.BasicAuth = YES;
//  Use the test Account SID and test Auth Token from the Twilio Console.
NSString *accountSid = @"YOUR_TEST_ACCOUNT_SID";
http.Login = accountSid;
http.Password = @"YOUR_TEST_AUTH_TOKEN";
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://api.twilio.com/2010-04-01/Accounts/"];
[url Append: accountSid];
[url Append: @"/Messages.json"];

//  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
req.HttpVerb = @"POST";
req.ContentType = @"application/x-www-form-urlencoded";
[req AddParam: @"From" value: @"+15005550006"];
[req AddParam: @"To" value: @"+12025550123"];
[req AddParam: @"Body" value: @"Hello from Twilio!"];

CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
success = [http HttpReq: [url GetAsString] request: req response: resp];
if ([resp.StatusCode intValue] == 0) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

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

    return;
}

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

if (([[reply JsonTypeOf: @"sid"] intValue] != 1) || ([[reply JsonTypeOf: @"status"] intValue] != 1)) {
    NSLog(@"%@",@"The create response is missing the message SID or status.");
    return;
}

NSLog(@"%@%@",@"Message SID: ",[reply StringOf: @"sid"]);
NSLog(@"%@%@",@"Initial status: ",[reply StringOf: @"status"]);