EmailSubscribeViewController to allow users in iOS applications to signup to a MailChimp Email List. This is a companion to my blog post http://kevinvanderlugt.github.io/ios-mailing-list-mailchimp/
#import <UIKit/UIKit.h> | |
@interface EmailSubscribeViewController : UIViewController <UITextFieldDelegate> | |
@end |
@interface EmailSubscribeViewController () | |
@property (weak, nonatomic) IBOutlet UITextField *emailField; | |
@property (weak, nonatomic) IBOutlet UITextField *firstNameField; | |
@property (weak, nonatomic) IBOutlet UITextField *lastNameField; | |
@end | |
@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 = @"https://API_ZONE.api.mailchimp.com/2.0/lists/subscribe"; | |
NSMutableURLRequest *urlRequest = | |
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] | |
cachePolicy:NSURLRequestUseProtocolCachePolicy | |
timeoutInterval:60.0]; | |
[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; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment