Swift Requires Chilkat v11.0.0+
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
let http = CkoHttp()!
http.requireSslCertVerify = true
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.authToken = "SENDGRID_API_KEY"
let resp = CkoHttpResponse()!
// Take the address from the authenticated account and the group ID from configuration.
var address: String? = "reader@example.com"
http.setUrlVar(name: "group", value: "12345")
success = http.httpNoBody(verb: "GET", url: "https://api.sendgrid.com/v3/asm/groups/{$group}", response: resp)
if success != true {
if resp.statusCode.intValue == 0 {
print("\(http.lastErrorText!)")
}
else {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
}
return
}
if resp.statusCode.intValue != 200 {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
return
}
let groupInfo = CkoJsonObject()!
success = groupInfo.load(json: resp.bodyStr)
if success != true {
print("Unexpected response: expected JSON.")
return
}
if groupInfo.string(ofEquals: "name", value: "Product updates", caseSensitive: true) != true {
print("Unexpected unsubscribe group; no preference was changed.")
return
}
let addresses = CkoJsonObject()!
addresses.updateString(jsonPath: "recipient_emails[0]", value: address)
success = http.httpJson(verb: "POST", url: "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions", json: addresses, contentType: "application/json", response: resp)
if success != true {
if resp.statusCode.intValue == 0 {
print("\(http.lastErrorText!)")
}
else {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
}
return
}
if resp.statusCode.intValue != 201 {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
return
}
success = http.httpJson(verb: "POST", url: "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search", json: addresses, contentType: "application/json", response: resp)
if success != true {
if resp.statusCode.intValue == 0 {
print("\(http.lastErrorText!)")
}
else {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
}
return
}
if resp.statusCode.intValue != 200 {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
return
}
let matches = CkoJsonArray()!
success = matches.load(jsonArray: resp.bodyStr)
if success != true {
print("Unexpected response: expected JSON.")
return
}
if matches.size.intValue == 1 {
print("Confirmed opt-out: \(matches.string(at: 0)!)")
}
else {
print("Opt-out not confirmed. Recheck this group.")
}
}