Swift Requires Chilkat v11.0.0+
Swift
Constant Contact V3: Create a contact
See more Constant Contact Examples
Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let vault = CkoSecrets()!
vault.location = "local_manager"
vault.autoSplit = true
let entry = CkoJsonObject()!
entry.updateString(jsonPath: "appName", value: "ConstantContactV3")
entry.updateString(jsonPath: "service", value: "OAuth2")
entry.updateString(jsonPath: "username", value: "my-account")
let tokens = CkoJsonObject()!
var loaded: Bool = vault.getSecretJson(jsonId: entry, jsonOut: tokens)
if loaded != true {
print("Run the desktop authorization example using this account and store.")
return
}
if tokens.jsonType(of: "access_token").intValue != 1 {
print("The saved authorization has no access token.")
return
}
let http = CkoHttp()!
http.requireSslCertVerify = true
http.authToken = tokens.string(of: "access_token")
let resp = CkoHttpResponse()!
let body = CkoJsonObject()!
body.updateString(jsonPath: "create_source", value: "Account")
body.updateString(jsonPath: "first_name", value: "Alex")
body.updateString(jsonPath: "last_name", value: "Rivera")
body.updateString(jsonPath: "email_address.address", value: "reader+newsletter@example.com")
// This sample assumes the account has recorded a qualifying existing relationship.
body.updateString(jsonPath: "email_address.permission_to_send", value: "implicit")
body.updateString(jsonPath: "street_addresses[0].kind", value: "work")
body.updateString(jsonPath: "street_addresses[0].street", value: "100 Main Street")
body.updateString(jsonPath: "street_addresses[0].city", value: "Boston")
body.updateString(jsonPath: "street_addresses[0].state", value: "MA")
body.updateString(jsonPath: "street_addresses[0].country", value: "United States")
success = http.httpJson(verb: "POST", url: "https://api.cc.email/v3/contacts", json: body, contentType: "application/json", response: resp)
if resp.statusCode.intValue == 409 {
print("Contact already exists. Look up the email address to obtain its ID.")
return
}
if (success != true) || (resp.statusCode.intValue != 201) {
print("Expected HTTP 201. Received \(resp.statusCode.intValue)")
if resp.statusCode.intValue == 401 {
print("Refresh the saved access token or authorize again.")
}
return
}
let created = CkoJsonObject()!
success = created.load(json: resp.bodyStr)
if success != true {
print("Expected a JSON response.")
return
}
if (created.jsonType(of: "contact_id").intValue != 1) || (created.string(ofEquals: "contact_id", value: "", caseSensitive: true) == true) {
print("Missing contact_id in the response.")
return
}
print("Created contact ID: \(created.string(of: "contact_id")!)")
}