[英]iOS Integrate Sendy API Into App
I am making an iPhone app that will need to communicate with the Sendy API . 我正在制作一个需要与Sendy API通信的iPhone应用程序。 I believe that it uses some kind of JSON, but I'm not really sure, nor do I know where to start.
我相信它使用某种JSON,但我不确定,也不知道从哪里开始。 I'm particularly interested in the
subscribe
portion of the API. 我对API的
subscribe
部分特别感兴趣。 Basically, I need to know how to talk to the Sendy API from my app. 基本上,我需要知道如何从我的应用程序与Sendy API通讯。
Any help is appreciated. 任何帮助表示赞赏。
My code: 我的代码:
- (IBAction)submitButtonPressed:(id)sender
{
self.data = [[NSMutableData alloc] initWithLength:0];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.erwaysoftware.com/sendy/subscribe"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"john@test.com" forHTTPHeaderField:@"email"];
[request setValue:@"john" forHTTPHeaderField:@"name"];
[request setValue:@"LxxxxxxxxxxxxxxxxxxxxQ" forHTTPHeaderField:@"list"];
[request setValue:@"true" forHTTPHeaderField:@"boolean"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.data appendData:d];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"")
otherButtonTitles:nil] show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
// Do anything you want with it
NSLog(@"%@", responseText);
}
When the log happens, the string is empty. 发生日志时,字符串为空。 I know through breakpoints that the last method is called.
我知道通过断点调用了最后一个方法。
Looking at the API it's all just plain text response. 看一下API,它们全都是纯文本响应。
Since it's a POST you can use an NSURLConnection
to compose the request. 由于它是POST,因此您可以使用
NSURLConnection
来撰写请求。 See this question for information on formatting the response. 有关格式化响应的信息,请参见此问题 。
An alternative is to use something like AFNetworking or RestKit that might be a little more friendly if you're doing more work with APIs. 一种替代方法是使用AFNetworking或RestKit之类的工具 ,如果您使用API进行更多工作,它们可能会更友好一些。
I'm guessing you've already resolved this but in case anyone else gets stuck here (as I did) I thought I'd post what I did to get it to work. 我猜您已经解决了这个问题,但是如果其他任何人都卡在这里(像我一样),我想我应该发布我所做的事情才能使它起作用。
The first thing you need to do is create a category called NSString+URLEncoding (or whatever) which is going to take your email and name fields from blah@blah.com and turn it into blah%40blah.com. 您需要做的第一件事是创建一个名为NSString + URLEncoding(或其他名称)的类别,该类别将从blah@blah.com中获取您的电子邮件和名称字段,并将其转换为blah%40blah.com。 I modified this from the handy blog post found here
我从这里的便捷博客文章中修改了此内容
@interface NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
@end
#import "NSString+URLEncoding.h"
@implementation NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
CFStringConvertNSStringEncodingToEncoding(encoding)));}
@end
Ok so now just import NSString+URLEncoding.h and add the following code and you'll be in business. 好的,所以现在导入NSString + URLEncoding.h并添加以下代码,您便可以正常工作。 This post helped me with this part
这篇文章对我有帮助
- (IBAction)submitButtonPressed:(id)sender
{
NSMutableURLRequest *newRequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://stashdapp.com/sendy/subscribe"]];
[newRequest setHTTPMethod:@"POST"];
[newRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *email = @"name@domain.com";
NSString *name = @"First Lastname";
NSString *list = @"XXXXXXXXXXXXXXXXXX";
NSString *postData = [NSString stringWithFormat:@"email=%@&boolean=true&name=%@&list=%@", [email urlEncodeUsingEncoding:NSUTF8StringEncoding],[name urlEncodeUsingEncoding:NSUTF8StringEncoding],list];
[newRequest setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:newRequest delegate:self];
[conn start];
}
You still include the delegate methods which you quoted in your question. 您仍然包括在问题中引用的委托方法。
Hope it helps someone! 希望它能对某人有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.