Objective-C Requires Chilkat v11.0.0+
Objective-C
Constant Contact V3: Create a contact
See more Constant Contact Examples
Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. 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>
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];
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"create_source" value: @"Account"];
[body UpdateString: @"first_name" value: @"Alex"];
[body UpdateString: @"last_name" value: @"Rivera"];
[body UpdateString: @"email_address.address" value: @"reader+newsletter@example.com"];
// This sample assumes the account has recorded a qualifying existing relationship.
[body UpdateString: @"email_address.permission_to_send" value: @"implicit"];
[body UpdateString: @"street_addresses[0].kind" value: @"work"];
[body UpdateString: @"street_addresses[0].street" value: @"100 Main Street"];
[body UpdateString: @"street_addresses[0].city" value: @"Boston"];
[body UpdateString: @"street_addresses[0].state" value: @"MA"];
[body UpdateString: @"street_addresses[0].country" value: @"United States"];
success = [http HttpJson: @"POST" url: @"https://api.cc.email/v3/contacts" json: body contentType: @"application/json" response: resp];
if ([resp.StatusCode intValue] == 409) {
NSLog(@"%@",@"Contact already exists. Look up the email address to obtain its ID.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] != 201)) {
NSLog(@"%@%d",@"Expected HTTP 201. Received ",[resp.StatusCode intValue]);
if ([resp.StatusCode intValue] == 401) {
NSLog(@"%@",@"Refresh the saved access token or authorize again.");
}
return;
}
CkoJsonObject *created = [[CkoJsonObject alloc] init];
success = [created Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON response.");
return;
}
if (([[created JsonTypeOf: @"contact_id"] intValue] != 1) || ([created StringOfEquals: @"contact_id" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Missing contact_id in the response.");
return;
}
NSLog(@"%@%@",@"Created contact ID: ",[created StringOf: @"contact_id"]);