Objective-C Requires Chilkat v11.0.0+
Objective-C
SendGrid: Save and verify a topic opt-out
See more SendGrid Examples
Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.
Add group suppressions ยท Check specific addresses
Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <NSString.h>
#import <CkoJsonObject.h>
#import <CkoJsonArray.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];
// Take the address from the authenticated account and the group ID from configuration.
NSString *address = @"reader@example.com";
[http SetUrlVar: @"group" value: @"12345"];
success = [http HttpNoBody: @"GET" url: @"https://api.sendgrid.com/v3/asm/groups/{$group}" 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] != 200) {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
return;
}
CkoJsonObject *groupInfo = [[CkoJsonObject alloc] init];
success = [groupInfo Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Unexpected response: expected JSON.");
return;
}
if ([groupInfo StringOfEquals: @"name" value: @"Product updates" caseSensitive: YES] != YES) {
NSLog(@"%@",@"Unexpected unsubscribe group; no preference was changed.");
return;
}
CkoJsonObject *addresses = [[CkoJsonObject alloc] init];
[addresses UpdateString: @"recipient_emails[0]" value: address];
success = [http HttpJson: @"POST" url: @"https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions" json: addresses 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;
}
success = [http HttpJson: @"POST" url: @"https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search" json: addresses 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] != 200) {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
return;
}
CkoJsonArray *matches = [[CkoJsonArray alloc] init];
success = [matches Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Unexpected response: expected JSON.");
return;
}
if ([matches.Size intValue] == 1) {
NSLog(@"%@%@",@"Confirmed opt-out: ",[matches StringAt: [NSNumber numberWithInt: 0]]);
}
else {
NSLog(@"%@",@"Opt-out not confirmed. Recheck this group.");
}