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

List Twilio message history

See more Twilio Examples

List inbound and outbound messages, newest sent date first. Print the Message SID, direction, and status so you can distinguish incoming messages from outbound submissions and see their latest state.

Each response contains a messages array and a next_page_uri. Follow that URI until it is null. PageSize limits one response. Use credentials for the account whose history you want to read.

Twilio Messages API reference

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <NSString.h>
#import <CkoStringBuilder.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;
//  Load the owning Account SID and an API key from application configuration.
NSString *accountSid = @"YOUR_ACCOUNT_SID";
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: accountSid];
[url Append: @"/Messages.json"];
[url Append: @"?PageSize=50"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
int total = 0;
BOOL more = YES;
while ((more == YES)) {
    success = [http HttpNoBody: @"GET" url: [url GetAsString] response: resp];
    if ([resp.StatusCode intValue] == 0) {
        NSLog(@"%@",http.LastErrorText);
        return;
    }

    BOOL parsed = [reply Load: resp.BodyStr];
    if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
        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: @"messages"] intValue] != 4) {
        NSLog(@"%@",@"Expected a messages array.");
        return;
    }

    int count = [[reply SizeOfArray: @"messages"] intValue];
    int i = 0;
    while ((i < count)) {
        reply.I = [NSNumber numberWithInt: i];
        NSLog(@"%@%@%@%@%@",[reply StringOf: @"messages[i].sid"],@"  ",[reply StringOf: @"messages[i].direction"],@"  ",[reply StringOf: @"messages[i].status"]);
        i = i + 1;
    }

    total = total + count;
    //  Twilio supplies a relative URI with the cursor and returns null on the last page.
    if ([reply IsNullOf: @"next_page_uri"] == YES) {
        more = NO;
    }
    else {
        if ([[reply JsonTypeOf: @"next_page_uri"] intValue] != 1) {
            NSLog(@"%@",@"Expected a next-page URI or null.");
            return;
        }

        [url SetString: @"https://api.twilio.com"];
        [url Append: [reply StringOf: @"next_page_uri"]];
    }

}

NSLog(@"%@%d",@"Messages found: ",total);