Objective-C Requires Chilkat v11.0.0+
Objective-C
Download the media files from a Twilio MMS
See more Twilio Examples
Supply the Message SID of a received or sent MMS. List its media records, then request each media resource without the .json extension to download the file bytes. Twilio may return a temporary HTTPS download URL. The example uses a separate HTTP client for that URL so the Twilio API key stays with the API host.
Run in an empty output directory. Files are written as mms-media-1.bin, mms-media-2.bin, and so on. The code prints the media SID and MIME type alongside each filename. An empty media list produces a count of zero.
Chilkat Objective-C Downloads
#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";
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://api.twilio.com/2010-04-01/Accounts/"];
[url Append: @"YOUR_ACCOUNT_SID"];
[url Append: @"/Messages"];
[url Append: @"/"];
[url Append: @"YOUR_MESSAGE_SID"];
[url Append: @"/Media"];
CkoStringBuilder *mediaBase = [[CkoStringBuilder alloc] init];
[mediaBase AppendSb: url];
[url Append: @".json"];
// Handle the API redirect explicitly so a signed download uses no Twilio credentials.
http.FollowRedirects = NO;
CkoHttp *download = [[CkoHttp alloc] init];
download.RequireSslCertVerify = YES;
CkoHttpResponse *fileResp = [[CkoHttpResponse alloc] init];
CkoStringBuilder *fileUrl = [[CkoStringBuilder alloc] init];
CkoStringBuilder *filename = [[CkoStringBuilder alloc] init];
CkoStringBuilder *mediaSid = [[CkoStringBuilder alloc] init];
int saved = 0;
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
BOOL more = YES;
while ((more == YES)) {
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: @"media_list"] intValue] != 4) {
NSLog(@"%@",@"Expected an array of media items.");
return;
}
int count = [[reply SizeOfArray: @"media_list"] intValue];
int i = 0;
while ((i < count)) {
reply.I = [NSNumber numberWithInt: i];
[mediaSid SetString: [reply StringOf: @"media_list[i].sid"]];
if ([mediaSid.Length intValue] == 0) {
NSLog(@"%@",@"Media item is missing its SID.");
return;
}
[fileUrl SetString: [mediaBase GetAsString]];
[fileUrl Append: @"/"];
[fileUrl AppendSb: mediaSid];
success = [http HttpNoBody: @"GET" url: [fileUrl GetAsString] response: fileResp];
if (([fileResp.StatusCode intValue] >= 300) && ([fileResp.StatusCode intValue] < 400)) {
[fileUrl SetString: [fileResp GetHeaderField: @"Location"]];
if ([fileUrl StartsWith: @"https://" caseSensitive: NO] != YES) {
NSLog(@"%@",@"Expected an HTTPS media download URL.");
return;
}
success = [download HttpNoBody: @"GET" url: [fileUrl GetAsString] response: fileResp];
}
if ((success != YES) || ([fileResp.StatusCode intValue] != 200)) {
NSLog(@"%@%@%@%d",@"Media download failed for ",[mediaSid GetAsString],@". HTTP ",[fileResp.StatusCode intValue]);
return;
}
[filename SetString: @"mms-media-"];
[filename AppendInt: [NSNumber numberWithInt: (saved + 1)]];
[filename Append: @".bin"];
success = [fileResp SaveBodyBinary: [filename GetAsString]];
if (success == NO) {
NSLog(@"%@",fileResp.LastErrorText);
return;
}
NSLog(@"%@%@%@%@%@",[filename GetAsString],@" ",[mediaSid GetAsString],@" ",[reply StringOf: @"media_list[i].content_type"]);
saved = saved + 1;
i = i + 1;
}
if ([reply IsNullOf: @"next_page_uri"] == YES) {
more = NO;
}
else {
[url SetString: [reply StringOf: @"next_page_uri"]];
if ([url StartsWith: @"/2010-04-01/Accounts/" caseSensitive: YES] != YES) {
NSLog(@"%@",@"Expected a Twilio relative next-page URI.");
return;
}
[url Prepend: @"https://api.twilio.com"];
}
}
NSLog(@"%@%d",@"Media files saved: ",saved);