Objective-C Requires Chilkat v11.0.0+
Objective-C
SendGrid: Create a newsletter draft, then schedule the reviewed draft
See more SendGrid Examples
Prepare a Marketing Campaigns design with a subject, content, and an unsubscribe module. Supply the design and list UUIDs, a verified marketing sender ID, and the unsubscribe group ID. The first run creates only a draft. After reviewing the audience and content, run the scheduling branch with that same ID. Single Sends use an ISO 8601 UTC string, unlike Mail Send’s numeric Unix timestamp.
Create a Single Send · Schedule a Single Send
Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <NSString.h>
#import <CkoJsonObject.h>
#import <CkoDateTime.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = @"SENDGRID_API_KEY";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
// Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
NSString *reviewedDraftId = @"";
if (reviewedDraftId == @"") {
CkoJsonObject *draft = [[CkoJsonObject alloc] init];
[draft UpdateString: @"name" value: @"November newsletter"];
[draft UpdateString: @"send_to.list_ids[0]" value: @"YOUR_LIST_UUID"];
[draft UpdateInt: @"email_config.sender_id" value: [NSNumber numberWithInt: 12345]];
[draft UpdateInt: @"email_config.suppression_group_id" value: [NSNumber numberWithInt: 67890]];
[draft UpdateString: @"email_config.design_id" value: @"YOUR_NEWSLETTER_DESIGN_UUID"];
success = [http HttpJson: @"POST" url: @"https://api.sendgrid.com/v3/marketing/singlesends" json: draft contentType: @"application/json" response: resp];
if (success != YES) {
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",http.LastErrorText);
}
else {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
}
return;
}
if ([resp.StatusCode intValue] != 201) {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
return;
}
CkoJsonObject *created = [[CkoJsonObject alloc] init];
success = [created Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Unexpected response: expected JSON.");
return;
}
NSString *draftId = [created StringOf: @"id"];
if ([created HasMember: @"id"] != YES) {
NSLog(@"%@",@"Missing id in response.");
return;
}
NSLog(@"%@%@%@",@"Draft created: ",draftId,@". Review it in Marketing Campaigns before scheduling.");
return;
}
// The example selects tomorrow at the current UTC time. Use your approved send time.
CkoDateTime *releaseTime = [[CkoDateTime alloc] init];
[releaseTime SetFromCurrentSystemTime];
[releaseTime AddDays: [NSNumber numberWithInt: 1]];
CkoJsonObject *schedule = [[CkoJsonObject alloc] init];
[schedule UpdateString: @"send_at" value: [releaseTime GetAsTimestamp: NO]];
[http SetUrlVar: @"draft" value: reviewedDraftId];
success = [http HttpJson: @"PUT" url: @"https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule" json: schedule contentType: @"application/json" response: resp];
if (success != YES) {
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",http.LastErrorText);
}
else {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
}
return;
}
if ([resp.StatusCode intValue] != 201) {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
return;
}
CkoJsonObject *scheduled = [[CkoJsonObject alloc] init];
success = [scheduled Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Unexpected response: expected JSON.");
return;
}
NSLog(@"%@%@%@",[scheduled StringOf: @"status"],@": ",[scheduled StringOf: @"send_at"]);