Sample code for 30+ languages & platforms
Objective-C 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 Objective-C Downloads

Objective-C
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoHttpRequest.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoSecrets *vault = [[CkoSecrets alloc] init];
vault.Location = @"local_manager";
vault.AutoSplit = YES;
CkoJsonObject *entry = [[CkoJsonObject alloc] init];
[entry UpdateString: @"appName" value: @"ConstantContactV3"];
[entry UpdateString: @"service" value: @"OAuth2"];
[entry UpdateString: @"username" value: @"my-account"];
CkoJsonObject *tokens = [[CkoJsonObject alloc] init];
BOOL loaded = [vault GetSecretJson: entry jsonOut: tokens];
if (loaded != YES) {
    NSLog(@"%@",@"Run the desktop authorization example using this account and store.");
    return;
}

if ([[tokens JsonTypeOf: @"access_token"] intValue] != 1) {
    NSLog(@"%@",@"The saved authorization has no access token.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [tokens StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];

CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"email" value: @"reader+newsletter@example.com"];
[query AddParam: @"status" value: @"all"];
success = [http HttpReq: @"https://api.cc.email/v3/contacts" request: query response: resp];
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
    NSLog(@"%@%d",@"Expected HTTP 200. Received ",[resp.StatusCode intValue]);
    if ([resp.StatusCode intValue] == 401) {
        NSLog(@"%@",@"Refresh the saved access token or authorize again.");
    }

    return;
}

CkoJsonObject *reply = [[CkoJsonObject alloc] init];
success = [reply Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Expected a JSON response.");
    return;
}

int count = [[reply SizeOfArray: @"contacts"] intValue];
if (count < 0) {
    NSLog(@"%@",@"The reply contained no contacts array.");
    return;
}

if (count == 0) {
    NSLog(@"%@",@"No contact has that email address.");
    return;
}

int i = 0;
while ((i < count)) {
    reply.I = [NSNumber numberWithInt: i];
    NSLog(@"%@%@",@"Contact ID: ",[reply StringOf: @"contacts[i].contact_id"]);
    NSLog(@"%@%@",@"Email permission: ",[reply StringOf: @"contacts[i].email_address.permission_to_send"]);
    if ([[reply JsonTypeOf: @"contacts[i].deleted_at"] intValue] == 1) {
        NSLog(@"%@%@",@"Deleted at: ",[reply StringOf: @"contacts[i].deleted_at"]);
    }

    i = i + 1;
}