ChimpKit2 是 MailChimp API 1.3 的简单 API 封装,用于与之交互。
一个 MailChimp 账户和 API 密钥。您可以在 此处 查看您的 API 密钥。
ChimpKit2 需要 ARC 和 iOS5。
通过以下方式将 ChimpKit2 添加为您 git 仓库的一个子模块:
cd myrepo
git submodule add https://github.com/mailchimp/ChimpKit2.git Lib/ChimpKit
ChimpKit2 请求设计为一次性使用。要发出一个请求,首先创建一个 ChimpKit 实例:
ChimpKit *ck = [[ChimpKit alloc] initWithDelegate:self
andApiKey:@"<YOUR_API_KEY>"];
您可能已经注意到,我们传递了 "self" 作为上面的委托。您应该实现 "ChimpKitDelegate" 协议,它包括以下方法:
- (void)ckRequestSucceeded:(ChimpKit *)ckRequest {
NSLog(@"HTTP Status Code: %d", [ckRequest responseStatusCode]);
NSLog(@"Response String: %@", [ckRequest responseString]);
}
- (void)ckRequestFailed:(NSError *)error {
NSLog(@"Response Error: %@", error);
}
获取数据就像在封装对象上调用 callApiMethod:withParams: 并传入所需的 API 方法名称一样简单。有关详细信息,请检查 API 文档。
您可以通过传递 "cancel" 消息来取消一个正在进行的请求。
[ck cancel];
ChimpKit2 默认为 10 秒的超时时间。您可以将这个(全局)更改为 30 秒,如下所示:
[ChimpKit setTimeout:30];
[ck callApiMethod:@"lists" withParams:nil];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:@"<YOUR_LIST_ID>" forKey:@"id"];
[params setValue:@"[email protected]" forKey:@"email_address"];
[params setValue:@"true" forKey:@"double_optin"];
[params setValue:@"true" forKey:@"update_existing"];
NSMutableDictionary *mergeVars = [NSMutableDictionary dictionary];
[mergeVars setValue:@"First" forKey:@"FNAME"];
[mergeVars setValue:@"Last" forKey:@"LNAME"];
[params setValue:mergeVars forKey:@"merge_vars"];
[ck callApiMethod:@"listSubscribe" withParams:params];
//You don't have to use a navigation controller, but we'll put a cancel button on it for you if you do
CKAuthViewController *authViewController = [[CKAuthViewController alloc] initWithClientId:@"<YOUR_CLIENT_ID>"
andClientSecret:@"<YOUR_CLIENT_SEEKRUT>"];
authViewController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:authViewController];
[self presentModalViewController:navigationController animated:YES];
您的委托必须实现 CKAuthViewControllerDelegate 协议。所需的方法是:
- (void)ckAuthSucceededWithApiKey:(NSString *)apiKey;
- (void)ckAuthFailedWithError:(NSError *)error;
您的委托可以选择实现:
- (void)ckAuthUserDismissedView;
如果您在乎当用户点击取消按钮时发生什么。