Swift Requires Chilkat v11.0.0+
Swift
Constant Contact V3: Create a contact list
See more Constant Contact Examples
Create a list with a unique name and a short description. Save the returned list_id with your application’s list record and use it in the membership examples. The new list starts empty. 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: "name", value: "Workshop announcements")
body.updateString(jsonPath: "description", value: "People who requested workshop updates")
success = http.httpJson(verb: "POST", url: "https://api.cc.email/v3/contact_lists", json: body, contentType: "application/json", response: resp)
if resp.statusCode.intValue == 409 {
print("A conflicting list already exists. Check the existing lists before retrying.")
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: "list_id").intValue != 1) || (created.string(ofEquals: "list_id", value: "", caseSensitive: true) == true) {
print("Missing list_id in the response.")
return
}
print("Created \(created.string(of: "name")!). List ID: \(created.string(of: "list_id")!)")
}