Objective-C Requires Chilkat v11.0.0+
Objective-C
Constant Contact V3: Add existing contacts to a list
See more Constant Contact Examples
Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat Objective-C Downloads
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <NSString.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];
// Use contact IDs from email lookups and the list ID from the selected destination.
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"source.contact_ids[0]" value: @"FIRST_CONTACT_ID"];
[body UpdateString: @"source.contact_ids[1]" value: @"SECOND_CONTACT_ID"];
[body UpdateString: @"list_ids[0]" value: @"DESTINATION_LIST_ID"];
success = [http HttpJson: @"POST" url: @"https://api.cc.email/v3/activities/add_list_memberships" json: body contentType: @"application/json" response: resp];
if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
NSLog(@"%@%d",@"Could not start the membership activity. HTTP ",[resp.StatusCode intValue]);
return;
}
CkoJsonObject *job = [[CkoJsonObject alloc] init];
success = [job Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON response.");
return;
}
if (([[job JsonTypeOf: @"activity_id"] intValue] != 1) || ([job StringOfEquals: @"activity_id" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Missing activity_id in the response.");
return;
}
NSString *activityId = [job StringOf: @"activity_id"];
NSLog(@"%@%@",@"Submitted activity ID: ",activityId);
[http SetUrlVar: @"job" value: activityId];
// Poll briefly. A longer-running job can be checked later using the same ID.
int attempt = 0;
while ((attempt < 5)) {
success = [http HttpNoBody: @"GET" url: @"https://api.cc.email/v3/activities/{$job}" response: resp];
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
NSLog(@"%@%d%@",@"Status check returned HTTP ",[resp.StatusCode intValue],@". Keep the activity ID and check again later.");
return;
}
success = [job Load: resp.BodyStr];
if ((success != YES) || ([[job JsonTypeOf: @"state"] intValue] != 1)) {
NSLog(@"%@",@"Missing activity state. Keep the activity ID.");
return;
}
NSString *state = [job StringOf: @"state"];
if ((state != @"initialized") && (state != @"processing")) {
NSLog(@"%@%@",@"Activity state: ",state);
int i = 0;
while ((i < [[job SizeOfArray: @"activity_errors"] intValue])) {
job.I = [NSNumber numberWithInt: i];
NSLog(@"%@",[job StringOf: @"activity_errors[i]"]);
i = i + 1;
}
return;
}
attempt = attempt + 1;
if (attempt < 5) {
[http SleepMs: [NSNumber numberWithInt: 2000]];
}
}
NSLog(@"%@",@"Still processing. Recheck the activity ID before submitting another request.");