Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
EmailSubscribeViewController to allow users in iOS applications to signup to a MailChimp Email List. This is a companion to my blog post
#import <UIKit/UIKit.h>
@interface EmailSubscribeViewController : UIViewController <UITextFieldDelegate>
@interface EmailSubscribeViewController ()
@property (weak, nonatomic) IBOutlet UITextField *emailField;
@property (weak, nonatomic) IBOutlet UITextField *firstNameField;
@property (weak, nonatomic) IBOutlet UITextField *lastNameField;
@implementation EmailSubscribeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
- (IBAction)backPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
- (IBAction)submitPressed:(id)sender {
// Get all the user inputs, no real validation is done here but you could
NSString *email = self.emailField.text;
NSString *firstName = self.firstNameField.text;
NSString *lastName = self.lastNameField.text;
// The MailChimp API that we are using was version 2.0, this may change
// API_ZONE should be replaced with your api zone
// This can be found at the end of your api key, mine was us8
NSString *urlString = @"";
NSMutableURLRequest *urlRequest =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPMethod:@"POST"];
// Create the body using the native json generator
NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
// Replace API_KEY with your api key found earlier
[body setObject:@"API_KEY" forKey:@"apikey"];
// Replace LIST_ID with your list ID
[body setObject:@"LIST_ID" forKey:@"id"];
NSMutableDictionary *emailDictionary = [[NSMutableDictionary alloc] init];
[emailDictionary setObject:email forKey:@"email"];
[body setObject:emailDictionary forKey:@"email"];
// Merge variables are values passed into your list including user's name
NSMutableDictionary *mergeVariables = [[NSMutableDictionary alloc] init];
if(firstName) {
[mergeVariables setObject:firstName forKey:@"FNAME"];
if(lastName) {
[mergeVariables setObject:lastName forKey:@"LNAME"];
[body setObject:mergeVariables forKey:@"merge_vars"];
// Optionally you can disable email verification
[body setObject:@NO forKey:@"double_optin"];
NSData *requestBody = [NSJSONSerialization dataWithJSONObject:body options:0 error:nil];
[urlRequest setHTTPBody:requestBody];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
if (error != nil) {
NSLog(@"Error subscribing to the list: %@", [error localizedDescription]);
} else {
// Do something useful here to thank your users
NSLog(@"Success, thanks for signing up!");
-(BOOL)textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];
return YES;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment