Objective-C Requires Chilkat v11.0.0+
Objective-C
SendGrid: Schedule a reminder and cancel it by batch ID
See more SendGrid Examples
Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.
Create a batch ID ยท Cancel a batch
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];
// On cancellation, set this to the batch ID saved with the original appointment.
// This branch does not create a new batch or send another reminder.
NSString *batchToCancel = @"";
if (batchToCancel != @"") {
CkoJsonObject *cancellation = [[CkoJsonObject alloc] init];
[cancellation UpdateString: @"batch_id" value: batchToCancel];
[cancellation UpdateString: @"status" value: @"cancel"];
success = [http HttpJson: @"POST" url: @"https://api.sendgrid.com/v3/user/scheduled_sends" json: cancellation 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;
}
NSLog(@"%@",@"Batch cancellation accepted.");
return;
}
success = [http HttpNoBody: @"POST" url: @"https://api.sendgrid.com/v3/mail/batch" 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 *batch = [[CkoJsonObject alloc] init];
success = [batch Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Unexpected response: expected JSON.");
return;
}
NSString *batchId = [batch StringOf: @"batch_id"];
if ([batch HasMember: @"batch_id"] != YES) {
NSLog(@"%@",@"Missing batch_id in response.");
return;
}
// Allocate one batch per independently cancelable appointment.
NSLog(@"%@%@",@"Save this batch ID with the appointment: ",batchId);
CkoDateTime *sendTime = [[CkoDateTime alloc] init];
[sendTime SetFromCurrentSystemTime];
[sendTime AddSeconds: [NSNumber numberWithInt: 3600]];
CkoJsonObject *message = [[CkoJsonObject alloc] init];
[message UpdateString: @"batch_id" value: batchId];
[message UpdateInt: @"send_at" value: [sendTime GetAsUnixTime: NO]];
[message UpdateString: @"from.email" value: @"sender@example.com"];
[message UpdateString: @"personalizations[0].to[0].email" value: @"recipient@example.com"];
[message UpdateString: @"subject" value: @"Appointment reminder"];
[message UpdateString: @"content[0].type" value: @"text/plain"];
[message UpdateString: @"content[0].value" value: @"Your appointment is tomorrow at 9:30 AM Central."];
success = [http HttpJson: @"POST" url: @"https://api.sendgrid.com/v3/mail/send" json: message 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] != 202) {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
return;
}
NSLog(@"%@%@",@"Reminder scheduled for ",[sendTime GetAsTimestamp: NO]);