Swift Requires Chilkat v11.0.0+
Swift
SendGrid: Look up a bounced email address
See more SendGrid Examples
Look up a recipient on the bounce suppression list without changing it. SendGrid returns an array. An empty array means there is no bounce record for this lookup; it does not prove a particular message reached the inbox.
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()!
// SetUrlVar encodes the raw address as a URL component, including the plus sign.
http.setUrlVar(name: "address", value: "avery+orders@example.com")
success = http.httpNoBody(verb: "GET", url: "https://api.sendgrid.com/v3/suppression/bounces/{$address}", 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 rows = CkoJsonArray()!
success = rows.load(jsonArray: resp.bodyStr)
if success != true {
print("Unexpected response: expected JSON.")
return
}
if rows.size.intValue == 0 {
print("No bounce record for this address.")
return
}
let row = CkoJsonObject()!
var i: Int = 0
while (i < rows.size.intValue) {
rows.objectAt2(index: i, jsonObj: row)
print("\(row.string(of: "email")!): \(row.string(of: "status")!) \(row.string(of: "reason")!)")
i = i + 1
}
}