Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

Constant Contact V3: Find a contact by email address

See more Constant Contact Examples

Look up an exact email address across contact states, including deleted contacts. Pass the address as a query parameter so a literal plus sign survives URL encoding. Read the stored contact ID, email permission, and deletion marker. An empty contacts array means no match. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat Swift Downloads

Swift

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 query = CkoHttpRequest()!
    query.httpVerb = "GET"
    query.addParam(name: "email", value: "reader+newsletter@example.com")
    query.addParam(name: "status", value: "all")
    success = http.httpReq(url: "https://api.cc.email/v3/contacts", request: query, response: resp)
    if (success != true) || (resp.statusCode.intValue != 200) {
        print("Expected HTTP 200. Received \(resp.statusCode.intValue)")
        if resp.statusCode.intValue == 401 {
            print("Refresh the saved access token or authorize again.")
        }

        return
    }

    let reply = CkoJsonObject()!
    success = reply.load(json: resp.bodyStr)
    if success != true {
        print("Expected a JSON response.")
        return
    }

    var count: Int = reply.size(ofArray: "contacts").intValue
    if count < 0 {
        print("The reply contained no contacts array.")
        return
    }

    if count == 0 {
        print("No contact has that email address.")
        return
    }

    var i: Int = 0
    while (i < count) {
        reply.i = i
        print("Contact ID: \(reply.string(of: "contacts[i].contact_id")!)")
        print("Email permission: \(reply.string(of: "contacts[i].email_address.permission_to_send")!)")
        if reply.jsonType(of: "contacts[i].deleted_at").intValue == 1 {
            print("Deleted at: \(reply.string(of: "contacts[i].deleted_at")!)")
        }

        i = i + 1
    }


}