Objective-C Requires Chilkat v11.0.0+
Objective-C
Constant Contact V3: Create a newsletter draft from an HTML file
See more Constant Contact Examples
Save your newsletter as UTF-8 in newsletter.html. Put the tracking tag inside the body, for example <body><h1>Workshop news</h1>[[trackingImage]]</body>. Supply a unique campaign name and confirmed sender and reply-to addresses for your account. Constant Contact uses the account’s physical mailing address in the footer by default.
This creates a custom-code draft. The example writes the campaign and primary activity IDs to newsletter-selection.json for the test and scheduling examples. If the response is lost after creation, look up the campaign by name before creating another draft.
Authorize the application using the same account and credential store.
Constant Contact developer guide
Chilkat Objective-C Downloads
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.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];
CkoStringBuilder *html = [[CkoStringBuilder alloc] init];
success = [html LoadFile: @"newsletter.html" charset: @"utf-8"];
if (success != YES) {
NSLog(@"%@",@"Could not read newsletter.html.");
return;
}
if ([html Contains: @"[[trackingImage]]" caseSensitive: YES] != YES) {
NSLog(@"%@",@"Add [[trackingImage]] inside the HTML body.");
return;
}
CkoJsonObject *draft = [[CkoJsonObject alloc] init];
[draft UpdateString: @"name" value: @"Workshop newsletter"];
[draft UpdateInt: @"email_campaign_activities[0].format_type" value: [NSNumber numberWithInt: 5]];
[draft UpdateString: @"email_campaign_activities[0].from_name" value: @"Workshop team"];
[draft UpdateString: @"email_campaign_activities[0].from_email" value: @"news@example.com"];
[draft UpdateString: @"email_campaign_activities[0].reply_to_email" value: @"workshops@example.com"];
[draft UpdateString: @"email_campaign_activities[0].subject" value: @"Upcoming workshops"];
[draft UpdateString: @"email_campaign_activities[0].html_content" value: [html GetAsString]];
success = [http HttpJson: @"POST" url: @"https://api.cc.email/v3/emails" json: draft contentType: @"application/json" response: resp];
if ((success != YES) && ([resp.StatusCode intValue] <= 0)) {
NSLog(@"%@",@"Create draft: HTTP request failed.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] != 201)) {
NSLog(@"%@%d%@%@",@"Create draft: HTTP ",[resp.StatusCode intValue],@" ",resp.BodyStr);
return;
}
CkoJsonObject *campaign = [[CkoJsonObject alloc] init];
success = [campaign Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON object in the response.");
return;
}
// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
CkoJsonObject *primary = [[CkoJsonObject alloc] init];
success = [campaign FindRecord2: @"campaign_activities" relPath: @"role" value: @"primary_email" caseSensitive: YES jsonObj: primary];
if ((success != YES) || ([[primary JsonTypeOf: @"campaign_activity_id"] intValue] != 1)) {
NSLog(@"%@",@"No primary email activity ID in the response.");
return;
}
NSString *selectedCampaign = [campaign StringOf: @"campaign_id"];
NSString *selectedActivity = [primary StringOf: @"campaign_activity_id"];
if ((selectedCampaign == @"") || (selectedActivity == @"")) {
NSLog(@"%@",@"Missing campaign or activity ID.");
return;
}
NSLog(@"%@%@",@"Campaign ID: ",selectedCampaign);
NSLog(@"%@%@",@"Primary activity ID: ",selectedActivity);
CkoJsonObject *selection = [[CkoJsonObject alloc] init];
[selection UpdateString: @"name" value: [campaign StringOf: @"name"]];
[selection UpdateString: @"campaign_id" value: selectedCampaign];
[selection UpdateString: @"campaign_activity_id" value: selectedActivity];
success = [selection WriteFile: @"newsletter-selection.json"];
if (success != YES) {
NSLog(@"%@",@"Could not save newsletter-selection.json. Keep the IDs printed above.");
return;
}
NSLog(@"%@",@"Saved newsletter-selection.json.");