Swift Requires Chilkat v11.0.0+
Swift
Constant Contact V3: Find a contact list by name
See more Constant Contact Examples
Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.
Authorize the application using the same account and 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()!
// Use the full display name. AddParam encodes spaces and punctuation.
var listName: String? = "Workshop announcements"
let query = CkoHttpRequest()!
query.httpVerb = "GET"
query.addParam(name: "name", value: listName)
query.addParam(name: "status", value: "active")
query.addParam(name: "include_membership_count", value: "active")
success = http.httpReq(url: "https://api.cc.email/v3/contact_lists", request: query, response: resp)
if (success != true) && (resp.statusCode.intValue <= 0) {
print("Look up list: HTTP request failed.")
return
}
if (success != true) || (resp.statusCode.intValue != 200) {
print("Look up list: HTTP \(resp.statusCode.intValue) \(resp.bodyStr!)")
return
}
let matches = CkoJsonObject()!
success = matches.load(json: resp.bodyStr)
if success != true {
print("Expected a JSON object in the response.")
return
}
if matches.size(ofArray: "lists").intValue == 0 {
print("No active email list named \(listName!)")
return
}
if (matches.size(ofArray: "lists").intValue != 1) || (matches.string(ofEquals: "lists[0].name", value: listName, caseSensitive: true) != true) {
print("Expected one exact list name.")
return
}
var listId: String? = matches.string(of: "lists[0].list_id")
if listId == "" {
print("Missing list ID.")
return
}
print("\(listName!) | List ID: \(listId!) | Mailable contacts: \(matches.int(of: "lists[0].membership_count").intValue)")
}