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

Schedule or cancel an SMS with Twilio

See more Twilio Examples

Live messaging operation. Choose one action per run. Scheduling requires an SMS-capable sender in a Twilio Messaging Service. Supply its MG SID, a recipient in E.164 format, and a send time from 15 minutes to 35 days in the future. The example uses UTC one hour from now. Twilio will send the scheduled message unless it is canceled in time.

Save the returned Message SID. To cancel later, select the cancel action and supply that SID under the same account. Check the returned status. If the message has already left the scheduled state, fetch its current status.

Twilio documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <CkoStringBuilder.h>
#import <CkoHttpRequest.h>
#import <CkoDateTime.h>
#import <NSString.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";

CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://api.twilio.com/2010-04-01/Accounts/"];
[url Append: @"YOUR_ACCOUNT_SID"];
[url Append: @"/Messages"];
//  Set this to true on a later run to cancel the saved message.
BOOL cancelMessage = NO;
CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
CkoDateTime *sendTime = [[CkoDateTime alloc] init];
req.HttpVerb = @"POST";
req.ContentType = @"application/x-www-form-urlencoded";
int expectedHttp = 201;
NSString *expectedStatus = @"scheduled";
if (cancelMessage != YES) {
    BOOL timeOk = [sendTime SetFromCurrentSystemTime];
    if (timeOk != YES) {
        NSLog(@"%@",sendTime.LastErrorText);
        return;
    }

    timeOk = [sendTime AddSeconds: [NSNumber numberWithInt: 3600]];
    if (timeOk != YES) {
        NSLog(@"%@",sendTime.LastErrorText);
        return;
    }

    [req AddParam: @"MessagingServiceSid" value: @"YOUR_MESSAGING_SERVICE_SID"];
    [req AddParam: @"To" value: @"RECIPIENT_E164_NUMBER"];
    [req AddParam: @"Body" value: @"Scheduled message."];
    [req AddParam: @"ScheduleType" value: @"fixed"];
    [req AddParam: @"SendAt" value: [sendTime GetAsTimestamp: NO]];
}
else {
    //  Update the individual message. This branch never creates another message.
    [url Append: @"/"];
    [url Append: @"SAVED_SCHEDULED_MESSAGE_SID"];
    [req AddParam: @"Status" value: @"canceled"];
    expectedHttp = 200;
    expectedStatus = @"canceled";
}

[url Append: @".json"];
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(@"%@",@"No HTTP response from Twilio.");
    return;
}

BOOL parsed = [reply Load: resp.BodyStr];
if ((success != YES) || ([resp.StatusCode intValue] != expectedHttp)) {
    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: @"sid"] intValue] != 1) || ([[reply JsonTypeOf: @"status"] intValue] != 1)) {
    NSLog(@"%@",@"The response is missing the message SID or status.");
    return;
}

NSLog(@"%@%@",@"Message SID: ",[reply StringOf: @"sid"]);
NSLog(@"%@%@",@"Current status: ",[reply StringOf: @"status"]);
if ([reply StringOfEquals: @"status" value: expectedStatus caseSensitive: YES] != YES) {
    NSLog(@"%@",@"The requested state was not confirmed. Check this SID before trying again.");
    return;
}

if (cancelMessage != YES) {
    NSLog(@"%@",@"Save the Message SID to check or cancel the scheduled message.");
}